C # string.split () Usage Summary. String.Split method (string[],? StringSplitOptions)

Source: Internet
Author: User

Split () is first a delimiter that splits a string into an array of characters according to the character in the split (' character '), and then saves it to a list object.

Output array objects often use foreach or for loops.

The first of these methods

string s=abcdeabcdeabcde; string [] Sarray=s.split ('C'); foreach (string in Sarray) Console.WriteLine (i.ToString ());

Output the following result:

Ab

Deab

Deab

De

The second method of

We see the result is a segmentation with a specified character. Use a different construction method for multiple words

to split the symbol :

 string  s= " ABCDEABCDEABCDE   "  string  [] Sarray1=s.split (new  char  [3 ]{"  c   ", "  d   ", "  e   " });  foreach  (string  i in   SArray1) Console.WriteLine (i.ToString ());  

You can output the following results

Ab

Ab

Ab

The third method of

string str1= i * * * * * * * * * * * * * * * * * * * teacher ; string [] str2;str1=str1. Replace (*****,*); str2=str1. Split (*); foreach (string in str2) Console.WriteLine (i.ToString ());

Output Result:

I

Is

One

A

Old

Division

Fourth type: String.Split method (string[],? StringSplitOptions):

Splits a string into multiple substrings based on a string in an array. You can specify whether the substring contains an empty array element

Separator
Type: system.string[]

A string array that separates substrings of this string, an empty array that does not contain delimiters, or null.

Options
Type: system.stringsplitoptions

stringsplitoptions. Removeemptyentries to omit empty array elements from the array returned; or stringsplitoptions. None to include empty array elements in the array returned. "  stringsplitoptions to omit empty array elements from the returned array. Removeemptyentries ; stringsplitoptions to include the empty array element in the returned array. None .

stringsplitoptions. Removeemptyentries to omit empty array elements from the array returned; or stringsplitoptions. None to include empty array elements in the array returned. " > instance:

This method can be used to return $$ characters $$ characters in exception:

 #regionGet error message/// <summary>       ///Get error message/// </summary>       /// <param name= "msg" ></param>       /// <returns></returns>       Private stringErrMsg (stringmsg) {           if(Msg. Contains ("$$"))               returnMsg. Split(new[] {"$$"}, Stringsplitoptions.none) [1]; Else               returnmsg; }       #endregion

When called:

Catch (Exception e)           {               thrownew  gspexception (errmsg (e.message), errorlevel.info);           }

The error message is handled by the ErrMsg method Exception e. E is written in database sql

if (V_count=0then        raise_application_error (-20001,'  $$ The document has been reviewed or executed and is not allowed to save $$');     End if;

This raise_application_error will return a bunch of strings in C # that contain $$ that the document has been audited or executed, and does not allow the $$ string to be saved . Includes various error messages, such as stack information.

But the foreground pop-up error when do not want other error message, just want to put the document has been audited or executed, not allow to save $$ removed as error message pop-up, this time you can intercept the error message.

First, according to the character $$ the error message to the division, the number of groups, and finally, will be divided into three elements of the array , the first is $$ front error message, the second is two $$ middle error message (This is what we want), the third is

The error message after the second $$. So we just get the second one, which is the array [1].

Msg. Split (New[] {"$$"}, Stringsplitoptions.none)[1];

This method can be called when the exception is processed.

C # string.split () Usage Summary. String.Split method (string[],? StringSplitOptions)

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.