Splitting a String


Suppose you have a list of names in a single string, with each name separated by a comma. Then suppose you wanted to extract each name from the string. The hard way to do this is to scan all the characters for commas, then copy the characters before the comma to another string. The easy way is to use the Split function in the string class. You can tell the Split function what character or characters to use as delimitersthe split function then creates substrings for each segment before and after each delimiter . The Split function returns an array of strings.

To split strings into an array of substrings:

  1. Type string[] pieces , where pieces is the name of the string array variable that will hold all the segments resulting from the split.

  2. Type = str.Split(','); where str is the name of the string variable containing the buffer you wish to split, and ',' is the delimiter character ( Figure 4.48 ).

    Figure 4.48 The Split function makes it easy to divide one string into multiple segments.
     string namelist= "Frodo,Gandalf,Aragorn,Samwise"; string[] names =  namelist.Split(',');  

graphics/tick.gif Tips

  • You can specify more than one delimiter to use in the split. For example if your string contains "Apples,Bananas,Grapes; Rice,Potatoes,Noodles;" you can specify both a single quote and a semicolon as delimiter characters ( Figure 4.49 ).

    Figure 4.49 The Split function is flexible enough to split based on any number of delimiters. In this example the function splits the original string each time it finds a comma or a semicolon.
     string food= "Apples,Bananas,Grapes; Rice,Potatoes,Noodles;"; string[] items =  food.Split(',',';');  //array contains six string elements. 
  • You can omit the delimiter character altogether, in which case the Split function treats spaces and carriage returns as delimiter characters ( Figure 4.50 ).

    Figure 4.50 If you don't specify a delimiter character, Split uses spaces and carriage returns as delimiters.
     string food="Apples Bananas Grapes Rice Potatoes Pizza"; string[] items =  food.Split();  foreach(string item in items) {    Response.Write(item + "<br>"); } /* prints out: Apples Bananas Grapes Rice Potatoes Pizza */ 
  • If the Split function finds two delimiter characters next to each other then the function returns an empty string to represent the segment between the two delimiters ( Figure 4.51 ).

    Figure 4.51 When you have two delimiters next to each other, in this case a space and a carriage return, you end up with an empty string.
     string food="Apples Bananas Grapes \nRice Potatoes Pizza"; string[] items =  food.Split();  foreach(string item in items) {    Response.Write(item + "<br>"); } /* outputs: Apples Bananas Grapes Rice Potatoes Pizza */ 
  • If you specify one or more delimiter characters in the Split function, and the Split function doesn't find any of the characters in the string, the Split function returns an array of one element in which the element is the entire original string ( Figure 4.52 ).

    Figure 4.52 If Split doesn't find the delimiter you specified, it simply returns an array with a single element containing the original string.
     string food="Apples,Bananas,Grapes,Rice,Potatoes,Pizza"; string[] items =  food.Split(';');  foreach(string item in items) {    Response.Write(item + "<br>"); } /* outputs: Apples,Bananas,Grapes,Rice,Potatoes,Pizza */ 



C#
C# & VB.NET Conversion Pocket Reference
ISBN: 0596003196
EAN: 2147483647
Year: 2003
Pages: 198
Authors: Jose Mojica

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