C # Summary of the Operation string method & lt; Convert & gt ;,

Source: Internet
Author: User

C # Summary of the Operation string method <>,

Staticvoid Main (string [] args)
{
String s = "";
// (1) character access (subscript access s [I])
S = "ABCD ";
Console. WriteLine (s [0]); // output "";
Console. WriteLine (s. Length); // output 4
Console. WriteLine ();

// (2) scatter as a character array (ToCharArray)
S = "ABCD ";
Char [] arr = s. ToCharArray (); // splits the string into character arrays {'A', 'B', 'C', 'D '}
Console. WriteLine (arr [0]); // outputs the first element of the array and outputs ""
Console. WriteLine ();

// (3) extract the Substring (Substring)
S = "ABCD ";
Console. WriteLine (s. Substring (1); // truncate from 2nd bits (index starts from 0) until the end of the string, and output "BCD"
Console. WriteLine (s. Substring (1, 2); // truncates two digits from the first digit and outputs "BC"
Console. WriteLine ();

// (4) matching index (IndexOf ())
S = "ABCABCD ";
Console. WriteLine (s. IndexOf ('A'); // search for the position index of the First Matching character A from the string header, and output "0"
Console. WriteLine (s. IndexOf ("BCD"); // search for the position of the first matching string BCD from the string header, and output "4"
Console. WriteLine (s. LastIndexOf ('C'); // search for the position of the First Matching character C from the end of the string, and output "5"
Console. WriteLine (s. LastIndexOf ("AB"); // search for the position of the first BCD matching string starting from the end of the string, and output "3"
Console. WriteLine (s. IndexOf ('E'); // search for the first matching string E position starting from the string header without matching the output "-1 ";
Console. WriteLine (s. Contains ("ABCD"); // checks whether another string "ABCD" exists in the string and outputs true
Console. WriteLine ();

// (5) Case sensitivity conversion (ToUpper and ToLower)
S = "aBcD ";
Console. WriteLine (s. ToLower (); // converts it to lowercase and outputs "abcd"
Console. WriteLine (s. ToUpper (); // convert to uppercase and output "ABCD"
Console. WriteLine ();

// (6) Fill alignment (PadLeft and PadRight)
S = "ABCD ";
Console. writeLine (s. padLeft (6, '_'); // use '_' to fill the left of the string and extend it to the total length of 6 characters. The output is "_ ABCD"
Console. writeLine (s. padRight (6, '_'); // use '_' to fill the right of the string and extend it to the total length of 6 bits. The output is "ABCD __"
Console. WriteLine ();

// (7) truncation and removal (Trim)
S = "_ AB __CD __";
Console. WriteLine (s. Trim ('_'); // remove the '_' character from the header and tail of the string and output "ABCD cd"
Console. WriteLine (s. TrimStart ('_'); // remove the '_' character from the header of the string and output "abcd cd __"
Console. WriteLine (s. TrimEnd ('_'); // remove the '_' character at the end of the string and output "_ ABCD cd"
Console. WriteLine ();

// (8) Insert and delete (Insert and Remove)
S = "ADEF ";
Console. WriteLine (s. Insert (1, "BC"); // Insert the string "BC" at the 2nd-bit position of the string and output "ABCDEF"
Console. WriteLine (s );
Console. WriteLine (s. Remove (1); // delete all the characters starting from the second character of the string and output ""
Console. WriteLine (s );
Console. WriteLine (s. Remove (0, 2); // Delete two characters starting from the 1st bits of the string and output "EF"
Console. WriteLine ();

// (9) Replace character (string) (Replace)
S = "A_ B _C_D ";
Console. WriteLine (s. Replace ('_', '-'); // Replace the '_' character in the string with '-', and output "A-B-C-D"
Console. WriteLine (s. Replace ("_", ""); // Replace "_" in the string with an empty string and output "a B C D"
Console. WriteLine ();

// (10) Split into a String Array (Split) -- reciprocal operation: Join a string static method (seperator, arr [])
S = "AA, BB, CC, DD ";
String [] arr1 = s. Split (','); // use the ',' character to separate the string and return an array of strings.
Console. WriteLine (arr1 [0]); // output "AA"
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 ('-'); // string-based splitting technique: First Replace the string "--" with a single character "-", then, the string is separated by the '-' character and an array of strings is returned.
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 ();

// (11) Format (static Format)
Console. WriteLine (string. Format ("{0} + {1} = {2}", 1, 2, 1 + 2 ));
Console. WriteLine (string. Format ("{0}/{1} = {. 000}", 1, 3, 1.00/3.00 ));
Console. WriteLine (string. Format ("{0: MM dd, yyyy}", DateTime. Now ));


// (12) connect to 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); // concatenates a string array into a string and outputs "ABCD"

Console. writeLine (string. join (",", arr3); // use "," as the separator to concatenate A string array into A string and output "A, B, C, D"

StringBuilder sb = new StringBuilder (); // declare a string constructor instance
Sb. Append ("A"); // use the string constructor to connect to the string for higher performance
Sb. Append ('B ');
Console. WriteLine (sb. ToString (); // output "AB"

Console. ReadKey ();
}

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.