C ++ Split separator cut string

Source: Internet
Author: User

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:
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.