Common ways to use C # string strings

Source: Internet
Author: User

1---The declaration of the string:

1, String S=new string (char[] arr)//The character array is converted to a string based on the declaration of a character string.

2, String s=new string (char r,int i)//generates the I-character R strings.

2---The usual static method of the string:

1. Comparison of Compare strings (in dictionary order)

int result= String.Compare (string str1,string str2);

When str1 > str2, return 1

   When str1 = str2, returns 0

When str1 < STR2, return-1

String.Compare (String str1,string str2,bool ignoreCase)//Ignore case comparison

2, concat connection method parameters are many, commonly used concat (string str1,string str2);

String str=string. Concat ("W", "E"); Str= "we";

3, format parameter processing, equivalent to Console.WriteLine ();

String Str=string.format ("Today {0} is very hot", "weather");//str= "The weather is very hot today";

4, IsNullOrEmpty judge whether the character is null or empty, the return value is bool;

String str1= "Hahha";

BOOL B1=string. IsNullOrEmpty (str);//b1=false;

String Str2= "";

BOOL B2=string. IsNullOrEmpty (str2);//b2=true;

String Str3=null;

BOOL B3=string. IsNullOrEmpty (STR3);//b3=true;

5. Merging of join strings

    string. Join (String str,string[] strarr); //Strarr The contents of the array into a new string and adds a delimiter between each of the corresponding array str

String strs=string. Join (",", string[]{"W", "E", "R", "T"});//strs= "W,e,r,t";

3---"string Common example method:

1. Contains determines whether a character is contained in a string and returns a bool value.

String str= "so Tired";

BOOL B=str. Contains ("tired");//b=true;

2. EndsWith and StartsWith determine if a string has started or ended

String Str= "The rain is so great";

BOOL B1=str. StartsWith ("large");//b1=false;

BOOL B2-str. EndsWith ("Ah");//b2=true;

3, Equals compares two strings for equality

String str1= "ASD";

String str2= "ERT";

BOOL B = str1.  Equals (STR2); B=false;

bool <strname>. Equals (String str, stringcomparison.ordinalignorecase)//indicates case insensitive

4, IndexOf and LastIndexOf determine where the string first appears (INDEXOF) and the last occurrence (LastIndexOf), and if not, the return value is-1

String str = "The rain is very big today, the weather is very cold";

int I=STR. IndexOf ("very"); i=4;

int I=STR. LastIndexOf ("very");//j=8;

int M=STR. IndexOf ("small");//m=-1;

5, replace the replacement string (the character is also possible), return the new string

String str= "so Sleepy";

String S=str. Replace ("Sleepy", "spirit");//s= "good spirit";

6. Insert Inserts

    Inserts a character at the index position of the string, and the original character moves back to a new string

String Str= "late at Night";

String S=str. Insert (1, "already");//s= "The Night is deep."

7. Remove delete string (character)

Removes the string starting from startindex, the length of which is long, and the remaining words conform to a new string (<strName> = <strname>. Remove (startindex,length);)

String Str= "The Night is Deep";

String S=str. Remove (//s=);

8, split the string <strName> in the sep array of characters, split the resulting content into an array (string[] <strname>. Split (params char[] sep);)

String Str= "I, really, very sleepy; yes";

String[] S=str. Split (new char () {', ', ', ', ', '} '),//s=string[]{"I", "true", "Good Sleepy", "yes"};

9. Substring Intercept character <str> start intercept with index, and intercept lenth characters (string <str>. Substring (Index,lenth))

String str= "still raining";

String S=str. Substring (2,2);//s= "Rain";

10, ToCharArray the string into a character array (<STRING>. ToCharArray ())

String Str= "The rain is already small";

Char[] S=str. ToCharArray ();//s=char[]{' rain ', ' already ', ' Jing ', ' small ', ' up '};

11, Trim () go out on both sides of the space

String str= "DD";

String S=str. Trim ();//s= "DD";

12, ToUpper (converted to uppercase) and tolower (converted to lowercase)

String s= "Raser";

String S1=s.toupper ();//s1= "Raser";

String S2=s.tolower ();//s2= "Raser";

    

    

    

Common ways to use C # string strings

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.