< experience >c#/. NET string manipulation methods Summary

Source: Internet
Author: User

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

1. Convert strings to List by delimiter

        /// <summary>          ///Convert a string to a List by delimiter/// </summary>          /// <param name= "str" >Source String</param>          /// <param name= "Speater" >Separators</param>          /// <param name= "ToLower" >Convert to lowercase</param>          /// <returns></returns>           Public Staticlist<string> Getstrarray (stringStrCharSpeater,BOOLtoLower) {            varList =Newlist<string>(); varSS =Str.            Split (Speater); foreach(varSinchSS) {                if(string. Isnullorwhitespace (s) | | s = = Speater. ToString (CultureInfo.InvariantCulture))Continue; varStrval =s; if(toLower) {Strval=S.tolower (); } list.            ADD (Strval); }            returnlist; }

2. Change the string to the data

        /// <summary>          ///Turn a string into a data partition/// </summary>          /// <param name= "str" ></param>          /// <returns></returns>           Public Static string[] Getstrarray (stringstr) {            returnStr. Split (New[] {',',',' }); }

3. Get the comma-delimited string for the array list

        /// <summary>        ///get a comma-delimited string of array lists/// </summary>        /// <param name= "list" ></param>        /// <param name= "Speater" ></param>        /// <returns></returns>         Public Static stringGetarraystr (list<string> list,stringspeater) {            varSB =NewStringBuilder ();  for(vari =0; I < list. Count; i++)            {                if(i = = list.) Count-1) {sb.                Append (List[i]); }                Else{sb.                    Append (List[i]); Sb.                Append (Speater); }            }            returnsb.        ToString (); }

4. Get the string length

        /// <summary>        ///Get string length/// </summary>        /// <param name= "Inputstr" ></param>        /// <returns></returns>         Public Static intStrlength (stringinputstr) {            varASC =Newasciiencoding (); vars =ASC.            GetBytes (INPUTSTR); returnS.sum (t = = T = = the?2:1); }

5. Intercept a specified length string

        /// <summary>        ///intercepts a specified length string/// </summary>        /// <param name= "inputstring" ></param>        /// <param name= "Len" ></param>        /// <returns></returns>         Public Static stringCUTSTR (stringInputString,intLen) {            varIsshowfix =false; if(Len%2==1) {Isshowfix=true; Len--; }            varASCII =Newasciiencoding (); varTemplen =0; varTempString =""; vars =ASCII.            GetBytes (inputstring);  for(vari =0; i < s.length; i++) {Templen+ = S[i] = = the?2:1; Try{tempstring+ = Inputstring.substring (i,1); }                Catch                {                     Break; }                if(Templen >len) Break; }            varMyByte =Encoding.Default.GetBytes (inputstring); if(Isshowfix && MyByte. Length >len) tempstring+="... .."; returntempstring; }

For the time being, there are five ways.

< experience >c#/. NET string manipulation methods Summary

Related Article

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.