StringsWhat 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.
ContainsTest 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.
StartsWithEndsWithYou 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})
IndexOfCODE
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.
LastIndexOfWhat 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
LengthSimple 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"
SubStringThis 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
ToCharArrayCopis the string to an Unicode character array.
They are a basic representation of a string, with out the functionality of the string type.
ToLowerSimply changes the string into lower-case letters.
CODE
a="ABC"
b=a.ToLower
'b="abc"
ToUpperSimply changes the string into upper-case (Capitals) letters.
CODE
a="abc"
b=a.ToUpper
'b="ABC"
TrimRemove 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.