String.Together Snazzy String Routines (Continued)
If you begin use String.Compare with any frequency, you're probably aware of the String.CompareTo method, which is immediately below it in the IntelliSense dropdown list. This is an instance method rather than a shared method. I prefer it in most cases because I think it makes the code a bit more readable, though opinions vary on that. You use this syntax when using CompareTo:
i = FirstString.CompareTo(SecondString)
CompareTo returns either -1, 0, or 1, just as Compare does. However it doesn't have an overload to ignore case during the comparison.
Classic VB developers are familiar with the LSet and RSet functions to pad strings with blanks. Both are still available in the Visual Basic compatibility library, but I recommend you use their .NET replacements, PadRight and PadLeft.
The names can be a bit confusing at first. PadRight corresponds to LSet, because LSet inserts any necessary spaces on the right hand side of the string. Similarly, PadLeft corresponds to RSet.
The main difference is that you can specify the padding character you want to use. For example, this code pads the string with number signs (hash marks):
Dim s As String = "abcdef"
Dim s2 As String
s2 = s.PadRight(10, "#"c)
' s2 now contains "abcdef####"
The argument for the pad character is of type Char; that's why the example includes a "c" after the pound sign. If you omit the "#"c pad character argument, .NET defaults to the space character.
The inverse of padding is trimming. VB .NET gives you three trimming methods: TrimStart, TrimEnd, and Trim. TrimStart trims at the beginning of the string, TrimEnd trims at the end of the string, and Trim combines those operations and trims both ends.
These methods correspond to the classic VB functions LTrim, RTrim, and Trim, which are still available in the compatibility library. But—and this is often the case—the .NET equivalent offers more functionality.
The old VB functions can trim only spaces, but the .NET equivalents incorporate a character array argument for you to specify any characters you want to trim. If you leave out that argument, the methods trim all whitespace by default.
For example, suppose you want to trim all punctuation from the end of a string. You could use TrimEnd with the punctuation characters specified:
Dim s As String = "You bet!!! "
Dim s2 As String
s2 = s.TrimEnd("!.? ".ToCharArray)
' s2 now contains "You bet"
s2 = s.TrimEnd() 'Default whitespace trimming
' s2 now contains "You bet!!!"
Searching Inside Strings
String.Contains is another nice addition to VB .NET's repertoire of String methods.
Classic VB developers are familiar with code that looks like this:
Dim s As String = "abcdef"
If InStr(s, "cd") <> 0 Then
MsgBox("Substring cd is present")
End If
As is usually with the classic syntax, this code still works if you take advantage of the compatibility library. But I much prefer the .NET alternative, String.Contains, if I'm just checking to see whether a substring is present:
Dim s As String = "abcdef"
If s.Contains("cd") Then
MsgBox("Substring cd is present")
End If
Back to top
|