String Manipulation

Creator:
Avtar

Throughout this tutorial I will use TheString to represent a string. Imagine it declared as this:

Dim TheString as string
The String = "Just some text stored in a string"

For some of the VB.NET examples I will use a different type of variable. It is a StringBuilder. The deceleration is such:

Dim StringB as New System.Text.StringBuilder()
StringB.append( "Just some text stored in a stringbuilder")

A string builder is much more efficient when changing the value of a string and much more feature rich. For more on why I used append here read the section on append. To return a real string from the StringBuilder you need to call the .ToString method:

MsgBox(StringB.ToString)

Almost all of the examples use a message box to display the results. Pasting the sample code and the decelerations into the load part of a form should render a working example.

Len or Length - Getting the length of a string

To get the length of the string in VB6 you would use the Len() function.

MsgBox(Len(TheString))

That will return 33 because TheString is 33 characters long.

In VB.NET you would use the .length property.

MsgBox(TheString.length)

This would also 33 because TheString is 33 characters long.

Mid or Substring - Returning part of a string

Using this method you can extract a string, a substring, from anywhere inside another string. In VB6 the Mid() function would normally be used (there are two others, Left() and Right(), but I will get to those latter).

MsgBox(Mid(TheString, 6, 4))

That will return "some". You told it to start at the sixth character and continue for the next four. The mid function works like this:

Mid(String, StartCharacter [, NumberOfCharacters])

For VB.NET you use the .substring method.

MsgBox(TheString.substring(5, 4))

If you noticed your probably wondering why I used 5 this time instead of 6. Well in VB.NET you start counting at 0 in a string, another step closer to the C languages. :P

Replace - When you have something you just don't want as is

This method is relatively simple. You give it a string to find and a string to replace the first string with. VB6 with Replace():

MsgBox(Replace(TheString, "text", "characters"))

The response from that should be "Just some characters stored in a string". VB.NET has little changes to it.

MsgBox(TheString.replace("text", "characters"))

This will also return "Just some characters stored in a string".

UCase or ToUpper - Making the text 'loud'

To change the case of some text to upper case or all capitals use the UCase() function.

MsgBox(UCase(TheString))

With the above code you would get "JUST SOME TEXT STORED IN A STRING". VB.NET with .toupper:

MsgBox(TheString.ToUpper)

Another way to get "JUST SOME TEXT STORED IN A STRING".

LCase or ToLower - Quiting down that 'loud' text

This is identically to UCase in VB6:

MsgBox(LCase(TheString))

With that you'd get "just some text stored in a string". The VB.NET method is also identically to .toupper:

MsgBox(TheString.ToLower)

Same as before "just some text stored in a string".

StartsWith - Matching the start of a string

This method has no VB6 equivalent, it is only available in VB.NET. This is general used in an If.

If TheString.StartsWith("Some") then
MsgBox("The string starts with 'Some'")
End If

That would return "The string starts with 'Some'".

EndsWith - Matching the end of a string

This method has no VB6 equivalent, it is only available in VB.NET. This is general used in an If.

If TheString.EndsWith("string") then
MsgBox("The string ends with 'string'")
End If

That would return "The string starts with 'string'".

Left or Substring - Getting the start of a string

This method has no true VB.NET equivalent, though substring replaced it. In VB6 you would use it as such:

MsgBox(Left(TheString, 4))

That would return "Some". In VB.NET you use the substring method and tell it to start at 0:

MsgBox(TheString.substring(0, 4))

This would also return "Some".

Right - Getting something from the end of a string

Just like Left, this method has no true VB.NET equivalent. You could through the use of .length and .substring avoid the use of this function but it's more work then you should need to do. VB6 with Right():

MsgBox(Right(TheString, 6))

This would return "string". In VB.NET you need to qualify the Right function because the Form object now has a Right property.

MsgBox(Microsoft.VisualBasic.Right(TheString, 6))

It's about twice as long as the VB6 example but does the same thing. I thought the .NET framework would make things easier? :P

Append - Adding on to a string

This function is only available to VB.NET using a stringbuilder. Append adds text to the string builder. A string builder does not allow assignment by use of an equal sign (=) so you must use the .append method to assign the initial value.

MsgBox(StringB.append(" object"))

Since StringB already contained the value of "Just some text stored in a stringbuilder" this would return "Just some text stored in a stringbuilder object".

StrReverse - gnirts a gnisreveR (Reversing a string)

This is a VB6 function. I have not found an equivalent VB.NET function.

MsgBox(StrReverse(TheString))

This would return the very confusing string of "gnirts a ni derots txet emos tsuJ".

Concat - Stick to string together

Well the VB6 verison of this is very simple and (in my opinion) much easier then this VB.NET function. VB6:

MsgBox(The String & " object")

That returns "Just some text stored in a string object". In VB.NET you could do this:

MsgBox(String.Concat(The String, " object"))

This returns the same as the much shorter and still useable in VB.NET VB6 code.

Split - When you need to separate some things

The split function is normally used to separate a comma (or other symbol) delimited list into an array. I'm not 100% sure this VB6 example is completely correct so don't think it's all your fault if you try it and it doesn't work.

Dim CSVlist as string
CSVlist = "part1|part2|part3|part4"
Dim CSVarray() as string
CSVarray = Split(CSVlist, "|")

That should set CSVarray to the following values:

CSVarray(0) = "part1"
CSVarray(1) = "part2"
CSVarray(2) = "part3"
CSVarray(3) = "part4"

In VB.NET you would use this:

Dim CSVlist as string
CSVlist = "part1|part2|part3|part4"
Dim CSVarray() as string
CSVarray = CSVlist.split("|")

This should have the same output as the VB6 example.

StrConv - Special string conversions

VB6 has some special string conversions. You can format a string so it is proper case. Proper case meaning the first letter of every word capitalized. This has no VB.NET equivalent.

MsgBox(StrConv(TheString, vbProperCase))

This would return "Just Some Text Stored In A String". You can replace vbProperCase with vbLowerCase for lower case conversion or vbUpperCase for upper case conversion.

Join - Reversing the splitting

Join can be used to reverse what split has done. It can take the members of an array and turn them in to a comma (or other symbol) delimited list. VB6:

Dim CSVarray() as string
CSVarray(0) = "part1"
CSVarray(1) = "part2"
CSVarray(2) = "part3"
CSVarray(3) = "part4"
Dim CSVlist as string
CSVlist = Join("|", CSVarray)

This should set CSVlist to a value of "part1|part2|part3|part4". The VB.NET version of this is:

Dim CSVarray() as string
CSVarray(0) = "part1"
CSVarray(1) = "part2"
CSVarray(2) = "part3"
CSVarray(3) = "part4"
Dim CSVlist as string = String.Join("|", CSVarray)

Trim - Cutin' off that white space

To remove extra white space (spaces) from the start and end of a string use the Trim() method in VB6.

Dim AString as string
AString = " Spaces "
MsgBox(Trim(AString))

This would return "Spaces". In VB.NET you'd use this:

Dim AString as string
AString = " Spaces "
MsgBox(AString.Trim)

LTrim, RTrim, TrimEnd, and TrimStart - Selectivly removing white space

LTrim and .TrimStart both remove white space from the start. VB6:

Dim AString as string
AString = " Spaces "
MsgBox(LTrim(AString))

This would return "Spaces ". In VB.NET you'd use this:

Dim AString as string
AString = " Spaces "
MsgBox(AString.TrimStrat)

To remove white space from the end use RTrim in VB6 and .TrimEnd in VB.NET.

InStr, InStrRev, IndexOf, and LastIndexOf - Finding the location of a substring in a string

These functions are used to find how many characters into a string a substring is found. VB6:

MsgBox(InStr(TheString, "text"))

This should return 11 because the t of text is 11 characters into the string. In VB.NET use the .indexof method.

MsgBox(TheString.IndexOf("text"))

To find the last occurrence of a substring in a string use the InStrRev function in VB6 or the LastIndexOf method in VB.NET.

Insert - Adding a string in

I am unaware of a VB6 equivalent though one may exist. In VB.NET you would do it as such:

MsgBox(TheString.insert(15, "and some more text "))

This should return a string of "Just some text and some more text stored in a string"

Format

Do to the large amount of information that I would need to dicuss to properly cover the format function it will not be discussed now.

Well I think I have covered enough for now. If you want a quick summary of everything covered here is a list of the VB6 and VB.NET equivalents:

  • Len = .length
  • Mid = .substring
  • Replace = .replace
  • UCase = .toupper
  • LCase = .tolower
  • N/A = .startswith
  • left = .substring
  • right = N/A
  • N/A = .append
  • StrReverse = N/A
  • N/A = .Concat
  • Split = .split
  • StrConv = N/A
  • Join = .Join
  • N/A = .endswith
  • RTrim = .TrimEnd
  • LTrim = .TrimStart
  • Trim = .Trim
  • InStr = .indexof
  • InStrRev = .lastindexof
  • N/A = .insert
Description:
There are many ways to manipulate a string. I have done my best to find them all and include them in this tutorial. First I will cover the VB6 method then I will have the VB.NET method.

Comments

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

More information about formatting options