Recipe 8.12. Converting Between Delimited Strings and Arrays


Problem

You have a string that contains data delimited by one or more characters, and you want to divide the parts into an array. Or you want to reverse the process, moving array elements into a delimited string.

Solution

Sample code folder: Chapter 08\SplitAndJoin

The Split() and Join() functions provided as part of the Visual Basic 2005 language, and the similar Split() and Join() methods of the string data type, provide a flexible and powerful way to manipulate string arrays.

Discussion

The Split() and Join() functions and methods are described in Chapter 5, which deals with strings, but here they are presented in the context of how they add useful functionality when working with string arrays.

Split() operates on a single string and returns a string array comprised of pieces of the original string split apart at the designated points. You can split the string at all occurrences of a given single character, at any occurrence of any single character in an array of characters, at any occurrence of any multicharacter string in a string array, or at any occurrence of a single multicharacter string. The overloaded versions of these methods provide considerable flexibility.

You do need to be careful when splitting a string at all occurrences of a single multi-character string. The string Split() method accepts a single string as the split parameter, but it uses only the first character of the string to define where to do the split. To use any multicharacter string for the split point, you must pass an array of strings instead of a single string. The string array can have just one string in it, but it must be an array in order to work as expected. (The Visual Basic 2005 Split() function doesn't have this limitation.)

To illustrate this, the following code splits a string at all occurrences of "en" and joins it again using Join(). The string to insert at the join points is "EN". This effectively uppercases all occurrences of "en" in the string. The string array splitArray() is the string array created by the split:

 Dim workText As String workText = _    "This sentence will have all ""en"" characters uppercased." Dim splitArray( ) As String = {"en"} Dim workArray( ) As String = _    workText.Split(splitArray, StringSplitOptions.None) workText = String.Join("EN", workArray) MsgBox(workText) 

Figure 8-12 shows the result.

Figure 8-12. Using Split() and Join( ) to replace all occurrences of a substring


There is a better way to replace all occurrences of a substring with another one: use the Replace( ) function. The following line of code has the same result as the previous code:

 workText = Replace(workText, "en", "EN") 


See Also

See Recipe 5.18 and Recipe 5.44 for more on the Split() and Join() functions and methods. Recipe 5.16 gives an example of using the Replace() function to replace all occurrences of a given substring.




Visual Basic 2005 Cookbook(c) Solutions for VB 2005 Programmers
Visual Basic 2005 Cookbook: Solutions for VB 2005 Programmers (Cookbooks (OReilly))
ISBN: 0596101775
EAN: 2147483647
Year: 2006
Pages: 400

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net