C # string method,

Source: Internet
Author: User

C # string method,

1 static void Main (string [] args) 2 {3 StrMethod (); 4} 5 6 public static void StrMethod () 7 {8 string myString = "brambling "; 9 string myNewStrUP = string. empty; 10 string myNewStrLow = string. empty; 11 string myStringL = string. empty; 12 string myStringR = string. empty; 13 14 for (int I = 0; I <myString. length; I ++) // like an array, you can use myString. length gets the Length of the string 15 {16 Console. writeLine ("character: {0}", mySt Ring [I]); // string type variables can be viewed as read-only arrays of char type variables 17} // Therefore, each character of the string (that is, element) can be accessed through subscript) 18 19 20 // However, this method cannot assign values to each character. To obtain a readable and writable char array, you can use myString. toCharArray () method 21 22 23 // myString. toCharArray () string to Char [] array 24 char [] myChar = myString. toCharArray (); 25 for (int I = 0; I <myChar. length; I ++) // if You Want To modify the character value, you cannot use foreach loop 26 {27 if (I = 0) 28 {29 myChar [I] = 'B'; // when I = 0 (the first letter of the string ), we changed it to 'B' 30} 31 Console in upper case. writeLine ("character: {0}", myChar [I]); 32} 33 34 35 // myString. toUpper () converts string to uppercase 36 myNewSt RUP = myString. toUpper (); 37 Console. writeLine ("{0} in upper case: {1}", myString, myNewStrUP); // output result: brambling in upper case: BRAMBLING 38 39 40 // myString. toLower () converts the string to the lower-case Form 41 myNewStrLow = myNewStrUP. toLower (); 42 Console. writeLine ("the lowercase characters of {0} are: {1}", myNewStrUP, myNewStrLow); // The output result is: brambling 43 44 45 string myStr = "str"; 46 string myNewStr = "--- str ---"; 47 string myStrL = string. empty; 48 String myStrR = string. empty; 49 string myNewStrL = string. empty; 50 string myNewStrR = string. empty; 51 Console. writeLine ("string 'str' length: {0}", myStr. length); // for better observation, use the following method to obtain the Length of the string. The space also has a Length and the result is: 7 52 53 54 // myStr. trimStart () removes the space character on the left by default when no parameter is available. If there is a parameter, removes the specified character on the left 55 myStrL = myStr. trimStart (); 56 Console. writeLine ("string: {0}", myStrL); 57 Console. writeLine ("String Length: {0}", myStrL. length); // remove the left space and the result is: 5 58 myNewStrL = myNewStr. trimStart ('-'); 59 Console. writeLine ("string: {0}", myNewStrL); // string "--- str ---" the new string after removing the '-' character on the left is: str --- 60 61 62 // myStr. trimEnd () removes the space character on the right by default when there is no parameter, and removes the specified character 63 myStrR = myStr when there is a parameter. trimEnd (); 64 Console. writeLine ("string: {0}", myStrR); 65 Console. writeLine ("String Length: {0}", myStrR. length); // remove the right space and the result is: 5 66 myNewStrR = myNewStr. trimEnd ('-'); 67 Console. writeLine ("string: {0} ", myNewStrR); // string" --- str --- "the new string after removing the '-' character on the right is: --- str 68 69 70 // You can also remove the spaces on both sides or specify the characters 71 // myStr. trim () removes the spaces between the left and right sides by default when there is no parameter, and removes the specified characters 72 myStr = myStr on both sides when there is a parameter. trim (); 73 Console. writeLine ("string: {0}", myStr); 74 Console. writeLine ("String Length: {0}", myStr. length); // remove the spaces between the left and right sides. The result is: 3 75 myNewStr = myNewStr. trim ('-'); 76 Console. writeLine ("string: {0}", myNewStr); // string "--- str ---" Remove the characters on both sides of the string '- The new string is str 77 78 79 // the above method is to remove the space character or the specified character, the following method adds a space before and after the string or the specified character 80/* myString. padLeft (10) when there is only one parameter, it indicates to fill the space on the left of the string until the length of the string is specified by the parameter. 81 when two parameters exist, the first parameter works the same as the other parameter, and the second parameter indicates filling in */82 Console with the specified character. writeLine ("String Length: {0}", myString. length); // first output the Length of the original string: 9 83 myStringL = myString. padLeft (10); // Add a space before the string. Parameter 10 indicates the length of the new string after filling 84 Console. writeLine ("String Length: {0}", myStringL. length); // The Length of the new string after the output is filled: 10 85 Console. writeLine ("String Length: {0}", myString); 86 Console. writeLine ("String Length: {0}", myString. padLeft (10, '-'); // fill the character '-' on the left of the string to make it up to 10 87 88 89 // myString. padRight (10) is used in the same way as myString. padLeft (10) only acts on the left, and one acts on the 90 Console on the right. writeLine ("String Length: {0}", myString. padRight (10, '-'); // fill the character '-' on the right of the string to make it up to 10 91 92 93 // s. split (',') splits the string into a string array 94 string s = "one, two, three"; 95 string [] arrStr = s. split (','); // use ',' to Split the string and return a string array 96 for (int I = 0; I <arrStr. length; I ++) 97 {98 Console. writeLine ("the {0} element of the string array is: {1}", I, arrStr [I]); // cyclically traverse the string array arrStr to output every string element 99} 100 101 102 // s. replace (",", "-") matches the character (or string) specified by the first parameter, and uses the character (or string) specified by the second parameter) replace 103 string sReplace = s. replace (',', '-'); 104 Console. writeLine ("the new string after string {0} is replaced: {1}", s, sReplace); 105 106 107 // s. insert (4, "four,") the first parameter indicates the start position of the Start insertion, and the second parameter indicates the string to be inserted. 108 string sInsert = s. insert (4, "four"); 109 Console. writeLine ("string {0} new string after insertion: {1}", s, sInsert); // The output result is one, four, two, three110 111 112 // s. remove (3) if there is only one parameter, it means to delete all the characters starting from the specified position to the last 113 string sRemove = s. remove (3); 114 Console. writeLine ("the new string after the string {0} is deleted: {1}", s, sRemove); // The output result is one115/s. remove (4, 4) When two parameters exist, the first parameter indicates the start position, and the second parameter indicates the number of characters deleted: 116 sRemove = s. remove (4, 4); 117 Console. writeLine ("the new string after the string {0} is deleted: {1}", s, sRemove); // The output result is one, three118 119 120 // StringBuilderl class, this class also has the Insert (), Remove (), Replace (), AppendFormat () methods, the use method is similar to the method of the String class 121 StringBuilder sb = new StringBuilder (); 122 sb. append ("one-two-three"); 123 sb. append ("-four"); // Append a string 124 Console after the string. writeLine (sb. toString (); // sb. toString () converts a String of the StringBuilder type to a String of the String type 125 126 127 // String. concat (arrStr) is a static method of the String class. This method has other 128 Console reloads. writeLine (String. concat (arrStr); // concatenates a String array into a String, and the output result is onetwothree129 130 131 // String. join (",", arrStr) is a static method of the String class. The first parameter is the specified delimiter, and the second parameter is a String array. This method has other 132 Console reloads. writeLine (String. join (",", arrStr); // concatenate a string array into a string with the specified delimiter. The output result is one, two, three1_134 135 // s. indexOf (",") searches for the location where the specified character or string appears for the first time in the string, and returns its index value. If not found,-1 136 Console is returned. writeLine ("the first comma is:" + s. indexOf (","); // The output result is: 3137 138 139 // s. lastIndexOf (",") searches for the last occurrence position of a specified character or string in the string and returns its index value. If not found,-1 140 Console is returned. writeLine ("the last comma is:" + s. lastIndexOf (","); // The output result is: 7141 142 143 // s. substring (4) This method has an overload. When there is only one parameter, it specifies the start position and is intercepted until 144 at the end of the string. // when there are two parameters, the first is the starting position of the specified string to be truncated, and the second is the 145 Console of the length of the specified string to be truncated. writeLine ("the truncated string is not specified:" + s. substring (4); // The output result is two, three146 string st = s. substring (s. indexOf (",") + 1, s. lastIndexOf (",")-(s. indexOf (",") + 1); 147 Console. writeLine ("the String with the specified truncation length is:" + st); // The output result is two148 149 150 // String. format () is a static method of the String class. Its function is to Format the String. This method has other methods for reloading the 151 Console. writeLine (String. format ("{0} + {1} = {2}", 1, 2, 1 + 2); 152 Console. writeLine (String. format ("{0}/{1} = {2: 0. 000} ", 1, 3, 1.00/3.00); // retain the three decimal places 153 Console. writeLine (String. format ("{0: MM dd, yyyy}", DateTime. now); 154 155 156 Console. readKey (); 157}

 

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.