C # collection String type (2)
In the previous article, I continued to talk about the String class.
6. Deleting strings
String deletion is implemented through the Remove method, in the format:
(1) string. Remove (start position)
(2) string. Remove (start position, Remove count)
The start position refers to the index of the string, which is an integer and smaller than the length of the string. The first format is to delete all sub-characters after the start position of the string, and the second is to delete the characters from the start position to the start position.
Example 6: delete a string 'str'
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 delstr2 = str after the string index is 6. remove (5, 5); // Delete the string index. The Console contains five characters starting from 5. writeLine (delstr1); Console. writeLine (delstr2); Console. readLine ();}}}
Output result: 012345
01234
7. Copying strings
The Copy and CopyTo methods are used to Copy strings. To Copy a String to another character array, use the static Copy method of String. The format is string. Copy (the string to be copied ).
The CopyTo method can implement the same Copy function, but has more functions. You can Copy part of the original string to a character array in the format of CopyTo (the starting position of the character to be copied, target character array, where the characters in the target array start to be stored and the number of characters to be copied ).
Example 7: copy a string 'str'
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 ();}}}
The output result is: This is a string
Is a string
8. String case-sensitive Conversion
String case conversion is implemented using the ToLower and ToUpper methods of the String class. The ToLower method converts a String to lowercase, And the ToUpper method converts the String to uppercase.
Example 8: converts the case of a string 'str'
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 format: {0}", lowerstr); Console. writeLine ("upper case: {0}", upperstr); Console. readLine ();}}}
The output result is: this is a string
THIS IS A STRING
9. string SEARCH
String search is implemented through the IndexOf method and LastIndexOf method. The format is:
String. IndexOf (the character or string to be searched)
String. LastIndexOf (the character or string to be searched)
Here, the IndexOf method returns the location where the character to be searched or the string appears for the first time, the LastIndexOf method is used to return the position of the character to be searched or the character string that was last found in the string to be searched. Both the IndexOf method and the LastIndexOf method return an integer. If the string to be searched does not contain the character or string to be searched, a negative number is returned.
Example 9: search string 'str'
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 ("the first occurrence of character I in the string str is: {0}", rh1); Console. writeLine ("the last occurrence of character I in the string str is: {0}", rh2);} else {Console. writeLine ("character I does not appear in the string str");} Console. readLine ();}}}
The output result is: the first occurrence of character I in the string str is: 2.
The last occurrence of character I in the string str is: 13
10. append the string
When using methods in the System. String class, you must create a new String object in the memory, which requires a new space for the new object. When you need to modify the String repeatedly, the system overhead associated with creating a new String object may be very high. To solve this problem, C # provides a StringBuilder class.
When using the StringBuilder class, you must first introduce the System. Text namespace and then initialize it using the new keyword. The StringBuilder class uses the same method as the String class.
Example 10: append the string 'str'
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 ("APPEND string: {0}", str); Console. readLine ();}}}
The output result is: --- Append ---
The appended string is Hellow World! What a beautiful day
Supplement: escape characters
The escape character has a specific meaning, which is different from the original meaning of the character. In C #, escape characters refer to "\", which is mainly used to indicate the control code that is inconvenient to represent with general characters.
The output C # language of escape characters has a special format:
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 to add @ Console. writeLine ("C :\\ Windows \ system32"); // The second output format is to change "\" to "\" Console. readLine ();}}}
The output result is: