The delimiter can be one of the following forms:
- Array with only one character:
- For example, for "A, B, C, D, E, F", you can use ',' or new [] {','}.
- Arrays with multiple characters:
- For example"A, B-C, D * E, F"Use new [] {','-'*'}:
- There is only one string array:
- For example "A => B => C => D => E => F"New [] {"=> "}
- Arrays with multiple strings:
- For example "A => B <= C => D <= E => F"New [] {"=>", "<= "}
Let's take a look at the example: string testString = "James Hare, 1001 Broadway Ave, St. Louis, MO, 63101"; // The passed split can be an array
String [] results = testString. Split (new [] {','}); // or pass a separate char under asp tutorial. net 4.0
Results = testString. Split (','); the result is as follows:
To treat the same string, we use ',' and '(Space ):
String testString = "James Hare, 1001 Broadway Ave, St. Louis, MO, 63101 ";
// Pass an array as the Separator
String [] results = testString. Split (new [] {',', ''});
// Or you can directly transfer it in 4.0
Results = testString. Split (',','');
The following result is displayed:
Use
StringThe usage as a Delimiter is basically the same:
string testString = "James Hare,,1001 Broadway Ave,St. Louis,MO,63101";
string[] results = testString.Split(new[] { ",," }, StringSplitOptions.None);
Result: You must provide
StringSplitOptionsThe enumeration type has two values:
- StringSplitOptions. None: Contains the empty array elements in the returned array.
- StringSplitOptions. RemoveEmptyEntries:The empty array element in the returned array is omitted.
Finally,
Split ()There is a form that allows you to limit the number of returned array elements. In this case
N-1Elements are generated based on your conditions. The last one is the rest of the unsplit part:
String testString = "James Hare, 1001 Broadway Ave, St. Louis, MO, 63101 ";
// Returns an array containing two elements.
String [] results = testString. Split (new [] {','}, 2, StringSplitOptions. None );
For (int I = 0; I <results. Length; I ++)
{
Console. WriteLine ("tElement {0}:" {1} "", I, results [I]);
}
The result of the limit is: