Common properties and methods for strings in C #

Source: Internet
Author: User
Tags lowercase

This article is a summary of the properties and methods we commonly use for string types in C #:


"1" Construction Method:

By constructing a method, we can build a string variable (or an object instance of string type) according to our own needs.


Case 1) by constructing a method, declaring and assigning a string consisting of 20 characters ' H ' and outputting it.

Code:

String Str=new string (' H ', 20); Creates a new object of type string and constructs a 20-h string with a constructor method (function) and assigns it to STR

Console.WriteLine (STR);

Console.readkey ();

Execution results:



Case 2) by constructing the method, constructs a character array {' H ', ' e ', ' l ', ' l ', ' o ', ', ', ', ', ', ', ', ', ', ', ', ', ', ', ', ' O ', ' r ', ' L ', ' d '

String Str=new string (new char[]{' H ', ' e ', ' l ', ' l ', ' l ', ' o ', ', ', ' W ', ' o ', ' r ', ' L ', ' d '});

Console.WriteLine (STR);

Console.readkey ();

Execution results:



"2" Length property to get the number of characters for the string.

Case 1) Gets the string "Hello,world." "Number of characters.

String str= "hello,world!";

Console.WriteLine ("\" hello,world!\ "number of characters: {0}", str.length);

Console.readkey ();


Execution results:



The "3" ToUpper () method, in which all lowercase letters in the string are converted to uppercase.

Case 1) The string "hello,world!" Converts a lowercase letter in uppercase and outputs a converted string.

String str= "hello,world!";

String Strnew=str.toupper ();

Console.WriteLine ("The string before conversion is: {0}", STR);

Console.WriteLine ("The converted string is: {0}", strnew);

Console.readkey ();

Execution results:



The "4" ToLower () method, in which all uppercase letters in a string are converted to lowercase.

Case 1) The string "hello,world!" Converts lowercase letters to uppercase and outputs the converted string.

String str= "hello,world!";

String Strnew=str.tolower ();

Console.WriteLine ("The string before conversion is: {0}", STR);

Console.WriteLine ("The converted string is: {0}", strnew);

Console.readkey ();


Execution results:



"5" compares two strings for the same, and you can select the comparison mode by parameter (case-insensitive or case-sensitive).

Case 1) Compare two string "hello,world!" in case-insensitive mode and "hello,world!".

String str1= "hello,world!";

String str2= "hello,world!";

case-insensitive mode

if (Str1.equals (str2,stringcomparison.ordinalignorecase))

{

Console.WriteLine ("The two strings that are compared are the same. ");

}

Else

{

Console.WriteLine ("The two strings compared are not the same. ");

}

Console.readkey ();


Execution results:



Case 2) Compare two string "hello,world!" in case-sensitive mode and "hello,world!".

String str1= "hello,world!";

String str2= "hello,world!";

case-sensitive mode

if (Str1.equals (str2,stringcomparison.ordinal))

{

Console.WriteLine ("The two strings that are compared are the same. ");

}

Else

{

Console.WriteLine ("The two strings compared are not the same. ");

}

Console.readkey ();

Execution results:



The "6" split () method, which splits the string, returns an array of string types.

Case 1) Split string "I am &c* #初 (Learn |) to form a" I am a C # beginner "string.

String str= "I am &c* #初 (Learn |);

Char[] chr={', ' & ', ' (', ' | ', '; '};

string[] strnew = Str.split (Chr, stringsplitoptions.removeemptyentries); The split string makes up a string array and removes the empty string

String str1= "";

for (int i=0;i<strnew.length;i++)

{

Str1+=strnew[i];

}

Console.WriteLine ("The string you want to get is: {0}", STR1);

Console.readkey ();


Execution results:




The "7" Substring () method intercepts the string and includes the indexed location at the time of interception.

Case one) intercept string "Everyone good, I am a C # beginner" in the "I am a C # beginner", and output before and after the interception of the string.

String strold= "Everybody good, I am a C # beginner";

String strnew=strold.substring (4);

Console.WriteLine ("The string before the interception is: {0}", Strold);

Console.WriteLine ("The string after the interception is: {0}", strnew);

Console.readkey ();


Execution results:



The "8" IndexOf () method is used to determine the index of the position of the first occurrence of a string in the string, and no return-1.

Case 1) Look for "C #" in, "Hello everyone, I am a C # beginner." "The position in the string. (in the first few characters).

String str1= "Hello everyone, I am a C # beginner." ";

int Position;

Position=str1.indexof ("C #");

Position+=1;

Console.WriteLine ("\" c#\ "in \" Everybody good, I am a C # beginner. \ {0} location. ", Position);

Console.readkey ();


Execution results:




The "9" LastIndexOf () method determines the index of the last occurrence of a string in the string, and returns 1.

Case 1 Output string "C #" in string "I love C #, you love C #, we all love C # language." The last occurrence in the.

String str= "I love C #, you love C #, we all love the C # language." ";

int Position;

Position=str.lastindexof ("C #");

Position+=1;

Console.WriteLine ("\" c#\ "in \" I love C #, you love C #, we all love C # language.) \ "Location {0}" appears, Position);

Console.readkey ();


Execution results:



"10" Determines whether the string starts with the specified substring.

Case 1) Judged "Everyone good, I am a C # beginner." Whether to start with "Everyone good", if it is the output is, if not the output is not.

String str= "Hello everyone, I am a C # beginner." ";

if (Str.startswith ("everyone good"))

{

Console.WriteLine ("Yes");

}

Else

{

Console.WriteLine ("not");

}

Console.readkey ();


Execution results:



"11" Determines whether the string ends with the specified substring.

Case 1) Judged "Everyone good, I am a C # beginner." "Whether the string is to". End

String str= "Hello everyone, I am a C # beginner." ";

if (Str.endswith) (". "))

{

Console.WriteLine ("Yes");

}

Else

{

Console.WriteLine ("not");

}

Console.readkey ();


Execution results:



"12" replaces the substring specified in the string with the string you want.

Case 1) will "everyone good, I am a C # beginner." "In the string." "Replace with". ”。

String str= "Hello everyone, I am a C # beginner." ";

String Strnew=str.replace (". ","。 ");

Console.WriteLine ("The string before substitution is: {0}", STR);

Console.WriteLine ("The replaced string is: {0}", strnew);

Console.ReadLine ();


Execution results:



The "13" Contains () method to determine whether the string contains the specified substring.

Case 1) to judge the string "Everyone good, I am a C # beginner." "String contains the substring" C # "If it contains the output" contains "if the output" does not contain "is not included.

String str= "Hello everyone, I am a C # beginner." ";

if (Str.contains ("C #"))

{

Console.WriteLine ("included.") ");

}

Else

{

Console.WriteLine ("not included.") ");

}

Console.readkey ();


Execution results:



"14" Trim () method, remove the space before and after the string.

Case 1) Remove the front and back spaces of the string "I am a C # beginner".

String Str= "I am a C # beginner";

String Strnew=str.trim ();

Console.WriteLine ("The string to remove before and after spaces is: {0}", STR);

Console.WriteLine ("Remove the string after the front and back space is: {0}", strnew);

Console.readkey ();


Execution results:



The "15" TrimStart () and TrimEnd () methods are to remove the space before the string and remove the space after the string.

Case 1) Remove "I am a C # beginner" and output separately.

String Str= "I am a C # beginner";

String Str1=str.trimstart ();

Console.WriteLine ("Remove the previous string as: {0}", STR);

Console.WriteLine ("Remove the space before the string: {0}", STR1);

String Str2=str1.trimend ();

Console.WriteLine ("Remove the space following the string: {0}", STR2);

Console.readkey ();


Execution results:



"16" string. IsNullOrEmpty () method to determine whether a string is empty.

Case 1) to determine if the "Hello" string is empty.

String str= "Hello";

if (string. IsNullOrEmpty (STR))

{

Console.WriteLine ("String is empty");

}

Else

{

Console.WriteLine ("String is not null");

}

Console.readkey ();


Execution results:



"17" string. Join () method, which joins the array in the specified string and returns a string.

Case 1, the string array {"Big", "Home", "good"} output as "big | home | good" form.

String[] str={"Big", "Home", "good"};

String strnew=string. Join ("|", STR);

Console.WriteLine (strnew);

Console.readkey ();


Execution results:


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.