Post by Bradley on Feb 6, 2005 20:03:49 GMT -5
String Manipulation
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.
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.
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?
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.