School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become an Expert!

Join 307,015 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 2,079 people online right now. Registration is fast and FREE... Join Now!




Strings

 
Reply to this topicStart new topic

> Strings

AdamSpeight2008
Group Icon



post 30 Mar, 2009 - 01:22 AM
Post #1



Strings

What is a String?

A string is a type that represents a block of characters, for example a message, a description.
It also has this basic set of methods and properties.

Contains
Test to see if a string contains a char or string.
{string}.Contains({Search String})
Example: Contains a "O"
CODE

Dim a As String="ABCDEFGHIJKLMNOPQRSTUVWXYZ"
IF a.Contains("O") Then
' The String Contains a Letter O
EndIf

Example: Contains the string "NOP"
CODE

Dim a As String="ABCDEFGHIJKLMNOPQRSTUVWXYZ"
IF a.Contains("NOP") Then
' The String Contains the string NOP
EndIf


The following to methods does the the same but for the beginning and end of string respectively.
StartsWith
EndsWith

You note that these only checks to see if the string contains the string, and doesn't return any information regarding its position within it.


Getting Positional Information
{string}.IndexOf({Search String})

IndexOf
CODE

p=a.IndexOf("ABC")
' P=0
p=a.IndexOf("XYZ")
'p = 23
p=a.IndexOf("DOG")
p=-1

So this return the position of the letter of the search string within the reference string, otherwise it return -1.
It also has a couple of other useful capabilities

{string}.IndexOf({Search String},{Start From})
CODE

p=a.IndexOf("XYZ",13)
' p = 23 Still

But
{string}.IndexOf({Search String},{Start From},{Length})
CODE

p=a.IndexOf("XYZ",13,6)
'p = -1

Why? We told it to search for "XYZ" from Index 13 upto Index (13+6) = 19.

LastIndexOf
What if you want to know the position of the last.
CODE

a="ABCABCABCABCAB"
p=a.LastIndexOf("ABC")
' p=9

So
CODE

p.LastIndexOf("ABC",6)
' p= 3

and
CODE

p.LastIndexOf("ABC",6,3)
' p= 3

but
CODE

p.LastIndexOf("ABC",0)
'p=-1


Length
Simple tells you the number of characters in the string.
CODE

a="ABCDEFGHIJKMLMNOPQRSTUVXYZ"
'a.Length = 26


Remove
{string}.Remove({Start From})
CODE

Dim b As String
b=a.remove(13)
' b="ABCDEFGHIJKLMN"

{string}.Remove({Start From},{Length})
CODE

Dim b As String
b=a.remove(13,4)
' b="ABCD"



Replace
{string}.Replace({Search String},{Replacement})
Replaces every occurrence of the search string with the replacement string.
CODE

a="ABCDEFGHIJLKMNOPQRSTUVWXYZNOP"
b=a.Replace("NOP","123")
' b="ABCDEFGHIJLKM123QRSTUVWXYZ123"


Split
{string}.Split({Split on Char})
This returns an array of strings based on the of split.
CODE

Dim s() As String
a="This is an example of string splitting"
s=a.split(" ")
' s(0)="This"
' s(1)="is"
' s(2)="an"
' s(3)="example"
' s(4)="of"
' s(5)="string"
' s(6)="splitting"

You may not have noticed but that results don't contain a space, so with other split char may not contain what you expected. See;
CODE

s()=a.split("s")
's(0)="Thi"
's(1)=" i"
's(2)=" an example of "
's(3)="tring "
's(4)="plitting"

and
CODE

s()=a.split("is")
's(0)="Th"
's(1)="s "
's(2)="s an example of str"
's(3)="ng spl"
's(4)="tt"
's(5)="ng"

So choose wisely.

To return a particular number of splits.
CODE

Dim sc() As Char = {Char.Parse(" ")}
s = a.Split(sc, 2)
' s(0)="This"
' s(1)="is"

It also possible to return based on multiple split chars.
CODE

  a = "This is an example of string splitting, another example"
  Dim sc() As Char = {Char.Parse(","), Char.Parse(" ")}
  s = a.Split(sc)

You note it return an empty result, to remove empties.
CODE

  a = "This is an example of string splitting, another example"
  Dim sc() As Char = {Char.Parse(","), Char.Parse(" ")}
  s = a.Split(sc, System.StringSplitOptions.RemoveEmptyEntries))


Insert
{string}.Insert({Start From},{Insert This String})
Place another string into the string at the specified position.
CODE

a = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
b = a.Insert(15, "123")
'b="ABCDEFGHIJKLMNO123PQRSTUVWXYZ"


SubString
This allows you to extract a block of character from a a string.
There are two ways it can be used.

{string}.SubString({Start From})
The first extracts the string from the index specified to the end of the string.
CODE

a="Dream In Code"
b=a.SubString(5)
'b = " In Code"


{string}.SubString({Start From},{Length})
The second extract the string from the index specified for selected number of characters.
CODE

a="Dream In Code"
b=a.SubString(5,4)
'b = " In "

Are you wondering this is like MID$, so where what about LEFT$ and RIGHT$,
With a little think you'll figure out the two following routines.
CODE

Private Function Left(ByRef S As String,ByVal c As Integer) As String
Return S.SubString(S,0,C)
End Function
Private Function Right(ByRef S As String,ByVal i As Integer) As String
Return S.SubString(S,i)
End Function



ToCharArray
Copis the string to an Unicode character array.
They are a basic representation of a string, with out the functionality of the string type.


ToLower
Simply changes the string into lower-case letters.
CODE

a="ABC"
b=a.ToLower
'b="abc"

ToUpper
Simply changes the string into upper-case (Capitals) letters.
CODE

a="abc"
b=a.ToUpper
'b="ABC"


Trim
Remove the specified chars from the start and end of the string.
Eg
CODE

a = "NABCDEFGHIJKMNOPQRSTUVXYZNOPN"
b = a.Trim("N")
'b="ABCDEFGHIJKMNOPQRSTUVXYZNOP"

Not specifying one removes trailing whitespace from beginning and end.
CODE

a="   ABCDEFGHIJKLMNOPQRSTUVWXYZ   "
b=a.Trim()
'b="ABCDEFGHIJKLMNOPQRSTUVWXYZ"


This should give you the basic understanding of the methods available on strings.
Go to the top of the page
+Quote Post


Register to Make This Ad Go Away!

crepitus
Group Icon



post 18 Sep, 2009 - 04:44 PM
Post #2
And remember folks,

Strings are immutable.

This means you cannot change a String, only create new ones. That's why this won't turn the sample String to upper case:

' How Not to do it:
Dim sample As String = "hello"
sample.ToUpper()


You have to instead say:

Dim sample As String = "hello"
sample = sample.ToUpper()


The ToUpper method cannot change sample, instead it creates a new String, which is then assigned to sample.

If you look at the other String methods above, you will see that Trim, ToLower, Insert, Replace and Remove all create new Strings.

It's a useful thing to remember for when you find that a String hasn't changed when you were expecting it to. And it might come up in an interview.
Go to the top of the page
+Quote Post

AdamSpeight2008
Group Icon



post 18 Sep, 2009 - 05:00 PM
Post #3
That's a good addition to the tutorial. icon_up.gif
Go to the top of the page
+Quote Post


Fast ReplyReply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 


Lo-Fi Version Time is now: 11/21/09 07:36AM

Live Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month