Overview
This article mainly explains the basic operation knowledge of string strings.
Reverse output
String str = Console.ReadLine (); for (int i = str.) Length-1; I >= 0; i--) { console.write (str[i]); } Console.ReadLine ();
Calculate the length of a string
String myString = "This is a test!"; Console.WriteLine ("text is: {0}", myString) Console.WriteLine ("Text ' s long is: {0}", Mystring.length)
Convert case
myString = Mystring.tolower (); Convert all characters to lowercase myString = Mystring.toupper (); Convert all characters to uppercase
Delete space before and after
myString = Mystring.trim (); Delete the space before and after the string char[] TrimChars = {', ' e ', ' s '}; Ready to delete the character myString = Mystring.trim (trimChars); Delete all specified characters myString = Mystring.trimend (); Delete the string after the space myString = Mystring.trimstart (); Remove spaces before a string
Add a space
myString = Mystring.padright (14, "); When the string is not 14 bits long, fill myString = Mystring.padleft (14, ") with the specified character on his right; When the string is not 14 bits long, fill with the specified character on his left
Splitting a string
string[] Nstrs = Mystring.split (", 3); Split by Space and returns the first three strings
Get substring
String a = Mystring.substring (2,2); Gets two characters starting from the third bit of the mystring string, because the index start bit is 0
Replacing characters in a string
String a = Mystring.replace ("i", "O"); Replace All "I" in this string with "O"
String is a read-only group of char variables
String myString = "This is a test!"; foreach (char mychar in myString) {Console.Write ("{0}", MyChar);}
An array of characters that can be read and written
char[] Mychars = Mystring.tochararray ();
How to represent special characters
Since double quotation marks are used in C # to break the beginning and end of a string, for some special characters, such as double quotation marks, you need to use auxiliary characters called escape characters to represent them.
Summarize
String operations are particularly important in programming and are the most frequently used, and need to master the common use of string operations
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Common uses of some string manipulations in C #