C # Action String method Summary < Go >

Source: Internet
Author: User

Staticvoid Main (string[] args) {            strings =""; //(1) Character access (subscript access s[i])s ="ABCD"; Console.WriteLine (s[0]);//output "A";Console.WriteLine (s.length);//Output 4Console.WriteLine (); //(2) break into character array (ToCharArray)s ="ABCD"; Char[] arr = S.tochararray ();//break the string into character array {' A ', ' B ', ' C ', ' D '}Console.WriteLine (arr[0]);//The first element of the output array, output "A"Console.WriteLine (); //(3) Intercept sub-string (Substring)s ="ABCD"; Console.WriteLine (S.substring (1));//start at 2nd bit (index starting from 0) intercept until end of string, output "BCD"Console.WriteLine (S.substring (1,2));//2 bits starting from 2nd bit, output "BC"Console.WriteLine (); //(4) matching index (IndexOf ())s ="ABCABCD"; Console.WriteLine (S.indexof ('A'));//search for the first matching character A's position index from the beginning of the string, output "0"Console.WriteLine (S.indexof ("BCD"));//Search the position of the first matching string BCD from the beginning of the string, output "4"Console.WriteLine (S.lastindexof ('C'));//search for the position of the first match character C from the end of the string, output "5"Console.WriteLine (S.lastindexof ("AB"));//Search from the end of the string for the position of the first matching string bcd, Output "3"Console.WriteLine (S.indexof ('E'));//The position of the first matched string e is searched from the beginning of the string, with no matching output "1";Console.WriteLine (S.contains ("ABCD"));//determines if another string "ABCD" is present in the string, outputting trueConsole.WriteLine (); //(5) Case conversion (ToUpper and ToLower)s ="ABcD"; Console.WriteLine (S.tolower ()); //convert to lowercase, output "ABCD"Console.WriteLine (S.toupper ());//convert to uppercase, output "ABCD"Console.WriteLine (); //(6) Fill alignment (PadLeft and PadRight)s ="ABCD"; Console.WriteLine (S.padleft (6,'_'));//fill the left part of the string with ' _ ' to extend it to 6-bit total length, output "__ABCD"Console.WriteLine (S.padright (6,'_'));//fill the right part of the string with ' _ ' to extend it to 6-bit total length, output "abcd__"Console.WriteLine (); //(7) Cutting head to Tail (Trim)s ="__ab__cd__"; Console.WriteLine (S.trim ('_'));//Remove the ' _ ' character from the string in the header and tail, output "AB__CD"Console.WriteLine (S.trimstart ('_'));//Remove the ' _ ' character of the header in the string and output "ab__cd__"Console.WriteLine (S.trimend ('_'));//Remove the trailing ' _ ' character from the string and output "__AB__CD"Console.WriteLine (); //(8) inserting and removing (insert and remove)s ="adef"; Console.WriteLine (S.insert (1,"BC"));//Insert the string "BC" in the 2nd place of the string, output "ABCDEF"Console.WriteLine (s); Console.WriteLine (S.remove (1));//from the beginning of the 2nd bit of the string to the last character is deleted, output "A"Console.WriteLine (s); Console.WriteLine (S.remove (0,2));//Remove 2 characters from the 1th bit of the string, output "EF"Console.WriteLine (); //(9) Replacement 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, output "A B C D"Console.WriteLine (); //(10) split into a string array (split)--reciprocal operation: union A string static method join (seperator,arr[])s ="AA,BB,CC,DD"; string[] arr1 = S.split (',');//splits a string with ', ' characters, returns an array of stringsConsole.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 ('-');//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 stringsConsole.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) 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)); //(12) 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=NewStringBuilder ();//declares a string constructor instanceSb. Append ("A");//using string constructors to connect strings for higher performanceSb. Append ('B'); Console.WriteLine (sb.) ToString ());//output "AB"Console.readkey (); }

C # Action String method Summary < forwarding;

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.