follow up on an article continue to say String class
Six, the deletion of the string
The deletion of a string is implemented by the Remove method in the following format:
(1) string. Remove (start position)
(2) string. Remove (start position, shift divisor)
Where the start position refers to the index of the string, is an integer, and is less than the length of the string. The first format is to delete all the child characters after the beginning of the string, and the second is to delete the character from the start position to the position where the divisor was moved.
Example six, implementing the deletion of the string str
<span style= "FONT-SIZE:18PX;" >using system;using system.collections.generic;using system.linq;using system.text;using System.Threading.Tasks; Namespace String { class program { static void Main (string[] args) { string str = "0123456789"; string delstr1; string delstr2; DELSTR1 = str. Remove (6);//delete the character after the string index is 6 delstr2 = str. Remove (5,5);//delete the string index from 5 onwards after the number of 5-length characters Console.WriteLine (DELSTR1); Console.WriteLine (DELSTR2); Console.ReadLine ();}}} </span>
The result of the output is: 012345
01234
Seven, copying of strings
The copy of the string is implemented using the Copy method and the CopyTo method. If you want to copy a string into another character array, you can use the static method copy of string to implement it. Its format is: string. Copy (the string to copy).
The CopyTo method can implement the same functionality as copy, but is more versatile and can copy part of the original string into a character array in the form of: CopyTo (the starting position of the character to copy, the target character array, the starting position in the target array, the number of characters to copy).
Example seven, implementing the copy of the string str
<span style= "FONT-SIZE:18PX;" >using system;using system.collections.generic;using system.linq;using system.text;using System.Threading.Tasks; Namespace String { class program { static void Main (string[] args) { string str = ' This is a string ';
string Copystr; Copystr = string. Copy (str); Char[] Newchar=new char[20]; Str. CopyTo (5,newchar,0,11); Console.WriteLine (COPYSTR); Console.WriteLine (Newchar); Console.ReadLine ();}}} </span>
The result of the output is: the is a string
is a string
Eight, case conversion of strings
string case conversions are implemented through the ToLower method and the ToUpper method of the String class, ToLower method is to convert the string to lowercase, and toupper to convert the string to uppercase.
Example eight, implementing case conversion of String str
<span style= "FONT-SIZE:18PX;" >using system;using system.collections.generic;using system.linq;using system.text;using System.Threading.Tasks; Namespace String { class program { static void Main (string[] args) { string str = ' This is a string ';
string Lowerstr; string upperstr; Lowerstr = str. ToLower (); Upperstr = str. ToUpper (); Console.WriteLine ("lowercase form: {0}", lowerstr); Console.WriteLine ("uppercase form: {0}", upperstr); Console.ReadLine ();}}} </span>
The result of the output is: the is a string
This is A STRING
Nine, String lookup
The search for strings is implemented through the IndexOf method and the LastIndexOf method. The format is:
string. IndexOf (the character or string to find)
string. LastIndexOf (the character or string to find)
Where the IndexOf method is to return the character or string to look for the first time the string to be found appears, the LastIndexOf method is to return the character or string to look for in the last occurrence of the string to be found in the position. Both the IndexOf method and the LastIndexOf method return an integer that returns a negative number if the character or string you are looking for is not included in the string you are looking for.
Example nine, implementation of string STR lookup
<span style= "FONT-SIZE:18PX;" >using system;using system.collections.generic;using system.linq;using system.text;using System.Threading.Tasks; Namespace String { class program { static void Main (string[] args) { string str = ' This is a string ';
int Rh1 = str. IndexOf ("i"); int RH2 = str. LastIndexOf ("i"); if (rh1>=0) { Console.WriteLine ("Character I in the string str first occurrence position is: {0}", RH1); Console.WriteLine ("Character I in the last occurrence of the string str is: {0}", RH2); } else { Console.WriteLine ("Character I is not present in string str"); } Console.ReadLine ();}}} </span>
The result of the output is: The first occurrence of the character I in the string str position is: 2
The last occurrence of the character I in the string str is: 13
10, append of String
When you use a method in the System.String class, you create a new string object in memory, which requires a new space to be allocated for the new object. The overhead associated with creating a new string object can be very high in situations where you need to perform repeated modifications to the string. To solve this problem, C # provides a class StringBuilder.
When you use the StringBuilder class, you first introduce the System.Text namespace and then initialize it with the new keyword. The method used for the StringBuilder class is the same as the method used for the string class .
Example 10, implementing the Append to the string str
<span style= "FONT-SIZE:18PX;" >using system;using system.collections.generic;using system.linq;using system.text;using System.Threading.Tasks; Namespace String { class program { static void Main (string[] args) { StringBuilder str =new StringBuilder ("Hellow world!"); Console.WriteLine ("---Append---"); Str. Append ("What a Beautiful Day"); Console.WriteLine ("appended string: {0}", str); Console.ReadLine ();}}} </span>
The result of the output is:---Append---
The appended string is: Hellow world! What a beautiful Day
supplemental: Escape character
The escape character has a specific meaning that differs from the character's original meaning. In the C # language, the escape character refers to "\", which is used primarily to denote control codes that are not easily represented by ordinary characters.
The output C # language for escape characters has a special format:
<span style= "FONT-SIZE:18PX;" >using system;using system.collections.generic;using system.linq;using system.text;using System.Threading.Tasks; Namespace String { class program { static void Main (string[] args) { Console.WriteLine (@ "C:\Windows \system32 ");//The first output format is the front plus @ Console.WriteLine (" C:\\Windows\\System32 ");//The second output format is to change" \ "to" \ " Console.ReadLine ();}}} </span>
The result of the output is:
C # Supplements's String Class (ii)