Welcome Guest!
Create Account | Login
Locator+ Code:

Search:
FTPOnline Channels Conferences Resources Hot Topics Partner Sites Magazines About FTP RSS 2.0 Feed

Click here to receive your FREE subscription to Visual Studio Magazine

String.Together Snazzy String Routines (Continued)

This code is cleaner, more understandable, and even easier to type than the Classic VB version.

ADVERTISEMENT

There's one key difference: You could also use Classic VB's Instr function to find the position of a substring. String.Contains returns only a Boolean indicating that the substring is present. You must use a different method of the String class, String.IndexOf, to find the position of a substring.

String.IndexOf behaves a lot like the Instr function, with some extra functionality thrown in. You need to be aware of one critical difference, however; Instr is one-based (the first character is considered to have the position one), while String.IndexOf is zero-based, so the first character is position zero. You can see the difference at work in this short snippet:

Dim s As String = "abcdef"
Dim n As Integer
n = InStr(s, "cd")  ' old form
' n is now 3
n = s.IndexOf("cd") ' new form
' n is now 2

This difference in position numbering shows up in a couple of other ways. If InStr doesn't find a substring, it returns 0. String.IndexOf can't do that because 0 means it found the substring in the first position. So, String.IndexOf returns -1 when it does not find the substring.

Position numbering also matters when you need to specify the starting position of the search. For example, this code performs a comparison of Instr and String.IndexOf, specifying where to begin looking for the substring:

Dim s As String = "abcdef"
Dim n As Integer
n = InStr(3, s, "cd")  ' old form
' n is now 3
n = s.IndexOf("cd", 3) ' new form
' n is now -1 because 3 is past the
' beginning of the substring

It's potentially confusing to move back and forth between one-based and zero-based functions, so I recommend that you drop Instr completely and replace it with String.Contains and String.IndexOf. That way, you can have access to the extra functionality in String.IndexOf. For example, you can specify the comparison (binary versus standard string) that you want to use.

It will also feel more natural to use a related method that offers another capability. String.IndexOfAny allows you to find the first location of any character in a character array:

Dim s As String = "abcdef"
Dim n As Integer
n = s.IndexOfAny("ec".ToCharArray)
' n is now 2 because the first character
' found (c) is in zero-based position 2

Concatenation Revisited
In the companion article to this one, I discussed how to use String.Join to assemble a string out of an array of smaller strings, with delimiters between each of the smaller strings. But if you want to do simple concatenation with no delimiters, you can use String.Concat. This method allows you to submit an array of strings or objects and get a concatenated string based on the array:

Dim sSubString(3) As String
sSubString(0) = "Nashville"
sSubString(1) = ", "
sSubString(2) = "TN"
sSubString(3) = " 37215"
Dim sCityState As String
sCityState = String.Concat(sSubString)
' sCityState now contains
' "Nashville, TN 37215"
Back to top













Java Pro | Visual Studio Magazine | Windows Server System Magazine
.NET Magazine | Enterprise Architect | XML & Web Services Magazine
VSLive! | Thunder Lizard Events | Discussions | Newsletters | FTPOnline Home