Summary of 5 C # string manipulation methods sharing

Source: Internet
Author: User
Tags split tostring

This article mainly introduces the summary of the 5 C # string manipulation methods to share, this article explained that the string to be converted to List by separator, the string to follow, split into data, get array list comma-delimited string, get string length and other methods, need friends can refer to the

String manipulation is the most basic, most common, and most commonly used in C #, and I've summarized several common methods here:

1. Convert string to List by separator

?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22-23 <summary>///converts strings to List///</summary>///<param name= "str" > Source string </param>///<par Am Name= "Speater" > Separator </param>///<param name= "ToLower" > Convert to lowercase </param>///<returns>< /returns> public static list<string> Getstrarray (String str, char Speater, bool toLower) {var List = new List< ;string> (); var ss = Str. Split (Speater); foreach (var s in ss) {if (string). Isnullorwhitespace (s) | | s = = Speater. ToString (CultureInfo.InvariantCulture)) continue; var strval = s; if (toLower) {strval = S.tolower ();} list. ADD (Strval); } return list; }

2. The string to follow, split into data

?

1 2 3 4 5 6 7 8 9 <summary>///the string to follow, split into data///</summary>///<param name= "str" ></param>///<returns& gt;</returns> public static string[] Getstrarray (String str) {return str. Split (new[] {', ', ', '}); }

3. Get array list comma-delimited string

?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22-23 <summary>///get array list comma-delimited string///</summary>///<param name= "list" ></param>///<param na Me= "Speater" ></param>///<returns></returns> public static string Getarraystr (list<string > list, String speater) {var sb = new StringBuilder (); for (var i = 0; i < list.) Count; i++) {if (i = = list. Count-1) {sb. Append (List[i]); } else {sb. Append (List[i]); Sb. Append (Speater); } return SB. ToString (); }

4. Get string length

?

1 2 3 4 5 6 7 8 9 10 11 <summary>///Get string length///</summary>///<param name= "inputstr" ></param>///<returns> </returns> public static int strlength (string inputstr) {var asc = new ASCIIEncoding (); var s = asc. GetBytes (INPUTSTR); Return S.sum (t => t = = 63? 2:1); }

5. Intercept the specified length string

?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 This is the

These five methods are temporary.

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.