Parsing of String class libraries

Source: Internet
Author: User

Example: string example = "Hello,world";

1. Property of String: Length: The user gets the lengths of the string: many times the function is to determine whether the length of the string is zero to determine whether a known object exists

Response.Write (example.  Length); Length is 11, note: subscript is 10, but the length is calculated starting from 1

2, the method of increasing the string:

A:insert (); Note: You must pass in two parameters, the first parameter is inserted from a position, the second parameter is inserted in the content

Response.Write (example. Insert (One, "How does You Do!")); The output is hello,world,how do do!

B:replace (); Note: You need to pass in two parameters, the first parameter is to find a character in the string that will be replaced, the first parameter is the character to be replaced

Response.Write (example. Replace ("World", "Provence")); The output is: hello,provence

C:string.  Join (); Note: You need to pass in two parameters, the first parameter is to split the connection with a string, the second argument is an array name

D: operator + number

3. How to delete a string:

A:remove (); Note: You can pass a parameter, you can also pass two parameters; A parameter represents the deletion of all subsequent characters from this subscript, and two parameters represent the deletion of a few characters from the subscript.

Response.Write (example.  Remove (5)); The result is: Hello

Response.Write (example. Remove (0,6)); The result: World

B:substring (); Note: You can pass a parameter, you can also pass two parameters, pass a parameter to intercept all the characters from this subscript, pass two parameters to intercept a few characters from this subscript

Response.Write (example. Substring (5)); The result is: the world

Response.Write (example. Substring (0,6)); The result is: Hello,

C:trim (); Note: You can pass in an array or do not pass in any parameters: remove spaces in a string

TrimEnd (); Remove whitespace from trailing string

TrimStart (); Remove whitespace from string header

4, the method of string modification:

A:toupper (); Convert all strings to uppercase

Response.Write (example.  ToUpper ()); The output is: Holle,world

ToLower (); Convert all strings to lowercase

Response.Write (example.  ToLower ()); The output is: "Hello,world

B:tochararray ();

5, the method of string query:

A:indexof (); Note: Pass a parameter if you find the subscript that returns the character, none returns-1

Response.Write (example.  IndexOf ("D")); return subscript as: 10

B:lastindexof (); Note: The index position of the last query begins to find

Response.Write (example. LastIndexOf ("H")); return subscript as: 0

C:lastindexofany (); Don't understand

D:endswith (); Query the end of a string there is no this character, there is a return true, none false

Response.Write (example.  EndsWith ("H")); Returns false

E:stractswith (); Query a string header there is no this character, there is a return true, none false

Response.Write (example.  Stractswith ("H")); Returns True

F:equals (); Determine if there is no this string, there is a return true, none false

Response.Write (example.  Equals ("H")); Returns false

Response.Write (example.  Equals ("Hello,world")); Returns True

G:string.  Equals (); Compares two strings for the same, returns true with different false

Response.Write (String.  Equals ("D", "D")); Returns True

Response.Write (String.  Equals ("D", "F")); Returns false

H:string.compare (); Compares the size between two strings to return 1, 0,-1

Response.Write (String.Compare (A, b)); A > B return 1

Response.Write (String.Compare (A, b)); A = b returns 0

Response.Write (String.Compare (A, b)); A < b return-1

6, splicing strings need to introduce using System.Text;

A:stringbuilder ();

StringBuilder sb= new StringBuilder ();

Sb. Append ("a");

Sb. Append ("B");

Sb. Append ("C");

Response.Write (sb.)  ToString ()); Output ABC

7. Format formatting (static method format)

Console.WriteLine (String. Format ("{0} + {1} = {2}", 1, 2, 1+2));
Console.WriteLine (String. Format ("{0}/{1} = {2:0.000}", 1, 3, 1.00/3.00));
Console.WriteLine (String. Format ("{0:yyyy mm month DD day}", DateTime.Now));

8, split as a string array (split)-reciprocal operation: union A string static method join (seperator,arr[])
         s = "Aa,bb,cc,dd";
         string[] arr1 = s.split (', '); //  splits the string with ', ' characters, returns an array of strings
         console.writeline (arr1[0]); //  output "AA"
        & nbsp Console.WriteLine (arr1[1]); //  output "BB"
         console.writeline (arr1[2]);  //  output "CC"
         console.writeline (arr1[3]); //  output "DD"
         console.writeline ();

s = "AA--BB--CC--DD";
string[] arr2 = S.replace ("--", "-"). Split ('-'); Tips for splitting with strings: first replace the string "--" with a single character "-" and then split the string with the '-' character to return an array of strings
Console.WriteLine (Arr2[0]); Output "AA"
Console.WriteLine (Arr2[1]); Output "BB"
Console.WriteLine (Arr2[2]); Output "CC"
Console.WriteLine (Arr2[3]); Output "DD"
Console.WriteLine ();

9. Concatenate into a string (static method Concat, static method join, and instance method Stringbuilder.append)
s = "A,b,c,d";
string[] Arr3 = S.split (', '); arr = {"A", "B", "C", "D"}

Console.WriteLine (String. Concat (ARR3)); Concatenate a string array into a string that outputs "ABCD"

Console.WriteLine (String. Join (",", ARR3)); Concatenate a string array into a string with "," as a split symbol, output "a,b,c,d"

StringBuilder SB =new StringBuilder (); Declares a string constructor instance
Sb. Append ("A"); Using string constructors to connect strings for higher performance
Sb. Append (' B ');
Console.WriteLine (sb.) ToString ());//Output "AB"

Parsing of String class libraries

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.