Strings that are defined using string cannot be modified after they are well defined. If you want to change, you must convert the original string to a character (char) array through the ToCharArray () function. It then transforms to form a new string.
The commonly used methods in strings are:
- ToLower (); Converts uppercase letters in a string to lowercase letters, and other characters unchanged.
- ToUpper (); Converts lowercase letters in strings to uppercase letters, and other characters unchanged
- S1. Equals (stringcomparison.ordinallgnorecase); case-insensitive comparisons.
- Contains (): whether to include substrings;
- SUBSTRING (): Intercept string
- LastIndexOf (): Find the index of the last string
- Split (): Remove extra characters
- Str.startswith (): Determines whether a string starts with a string
- Str.endswith (): Determines whether a string ends with a string
- Str. IndexOf (): Displays the index of the string, if the string is not found, the returned result is-1
Let me give you a few examples to illustrate the effect of the above string methods:
1. Case-insensitive comparisons
#regionIgnore case comparisonConsole.WriteLine ("Please enter the first course:"); stringSTR3 =Console.ReadLine (); Console.WriteLine ("Please enter the second course;"); stringSTR4 =Console.ReadLine (); BOOLresult =STR3. Equals (STR4, stringcomparison.ordinalignorecase); //ignores case comparisons, where STR4 represents the string to compare, and stringcomparison.ordinalignorecase means to compare it in a case-insensitive manner. if(Result) {Console.WriteLine ("courses like"+STR3); } Else{Console.WriteLine ("Course not the same {0}_________{1}", STR3, STR4); } console.readline (); #endregion
2. Remove the characters that are not required in the string.
#regionRemove unwanted charactersstringSTR5 ="I have a friend, he------------called Xiao---------Ming";//This is a string containing impurities, I want to put the space and "-" to remove Char[] Chs1 =New Char[] {' ','-'};//This indicates the impurity character to be removed. string[] Result1 =STR5. Split (CHS1, stringsplitoptions.removeemptyentries); /** In the previous line of code, there is no empty element character in the returned value because it is the reason for stringsplitoptions.removeemptyentries. If you want to select Stringsplitoptions.none, you want to return * characters with empty elements. The processing of these two methods cannot be seen in the Console.Write (), but the difference can be seen in Console.WriteLine (). */ for(inti =0; I < RESULT1. Length; i++) {Console.Write (result1[i]); } console.readline ();#endregion
3. Replace certain characters or strings in a string with other characters or strings
#regionReplaces certain characters or strings in a string with other characters or strings.stringstr ="Lao Yang is evil ."; STR= str. Replace ('very','No'); Console.WriteLine (str); Console.ReadLine (); STR= str. Replace ("Lao Yang","Old Sue"); Console.WriteLine (str); Console.ReadLine (); #endregion
4. Determine if the string contains the string to be checked
#region Determines whether the string contains the string to be checked. string" Xiao Yang is evil "; bool result = str. Contains (" Xiao Yang "); if (Result) { Console.WriteLine (result); Console.ReadLine (); } #endregion
5. Find the index of a character in the Intercept
#regionIndex of a character found in interceptstringstr ="Xiao Yang is evil ."; STR= str. Substring (3);//intercept the character, and start intercepting it where the character is labeled 3, preserving what is behind. , but the subscript cannot exceed the string length. Console.WriteLine (str); Console.ReadLine (); STR="Xiao Yang is evil ."; STR= str. Substring (3,2);//indicates that the third character starts and then three characters are intercepted. The subscript for two values cannot exceed the character lengthConsole.WriteLine (str); Console.ReadLine (); /***********************************************************************************************************/ //Intercept path h:\ learning materials \ Accelerated \cs learning \ Wisdom Podcast Basic Training 3\20121102c# basics \ Video \22 methods to remove strings. 22 Methods for removing strings in avi. avi stringPath =@"h:\ learning materials \ Accelerated \cs learning \ Wisdom Podcast Basic Training 3\20121102c# basics \ Video \22 methods for removing strings. avi";//here to use @,//Method OnePath = path. Substring (path. Length- -); Console.WriteLine (path); Console.ReadLine (); //Method Two: Char[] CHS =New Char[1] {'\\' }; string[] path1 =path. Split (CHS); Console.WriteLine (path1[path1. Length-1]); Console.ReadLine ();#endregion
6. Determine whether a string starts or ends with a string
#regionDetermines whether a string starts or ends with a string.stringstr ="Xiao Yang is very pure ."; BOOLresult = Str. StartsWith ("Xiao Yang"); if(Result) {Console.WriteLine ("It starts with this string."); Console.ReadLine (); } result= str. EndsWith ("Purity"); if(Result) {Console.WriteLine ("This is the end of the string."); Console.ReadLine (); }#endregion
Two. NET common basic Class Library "2.1" string processing