Strings are a lot of data types used in programs and are the most commonly used reference types. The String class belongs to the System namespace and is a class library dedicated to processing strings provided by. NET Framework. The following describes common string processing methods:
Common string processing methods: bool Equals (string str) and "=" have the same effect. It is used to compare whether two strings are equal. If they are equal, true is returned. Otherwise, false is returned.
ToLower () returns the string in lowercase.
ToUpper () returns the string in uppercase.
Trim () removes spaces at both ends of the string
Substring (int a, int B) retrieves a Substring whose length is B from the specified position a of the string
Int IndexOf (string str) gets the index of the first matching item of the specified string str in the current string. If a matching item exists, the index is returned, and-1 is returned if no matching item exists.
Int LastIndexOf (string str) gets the index of the last matching item of the specified string str in the current string. If a matching item exists, the index is returned, and-1 is returned if no matching item exists.
String [] Split (char separator) Splits a string using the specified separator and returns an array composed of the Split strings.
String Join (string sep, string [] str) each character in the string array 'str' is connected using the specified delimiter 'sep'. Return the connected string.
Int Compare (string s1, string s2) compares the two strings and returns an integer. If s1 is less than s2, the return value is less than 0. If s1 is greater than s2, the return value is greater than 0. If s1 is equal to s2, the return value is 0.
Replace (string oldV, string newV) Replace the oldV value with the value of newV
The following is an example of how to handle some strings:
Problem description:
Enter an email address to obtain the email user name;
Enter a string with spaces to separate and connect;
Enter uppercase letters and convert them to lowercase letters.
Code implementation:
Using System; using System. collections. generic; using System. linq; using System. text; using System. threading. tasks; namespace Stringchuli {class Program {static void Main (string [] args) {string strname; string inputStr; string [] splitString; string joinString; string strEnglish; string email; Console. writeLine ("enter a new email address:"); email = Console. readLine (). trim (); Console. writeLine ("your email is {0}", email );/ /The user name int intindex = email. indexOf ("@"); if (intindex> 0) {strname = email. substring (0, intindex); // output the mailbox user name Console. writeLine ("your username is {0}", strname);} else {Console. writeLine ("the format you entered is incorrect! ");} Console. writeLine ("enter a string and separate words with spaces:"); inputStr = Console. readLine (); Console. writeLine ("the string you entered is {0}", inputStr); // use a space to separate the string splitString = inputStr. split (''); // output the Split string Console. writeLine ("the split string is:"); foreach (string s in splitString) {Console. writeLine (s);} // The split string uses-to connect joinString = string. join ("-", splitString); // output the connected string Console. writeLine ("the connected string is {0}", joinString); Console. writeLine (" Enter an uppercase English string: "); strEnglish = Console. readLine (); Console. writeLine ("your input capital string is {0}", strEnglish); // converts the input capital string to a lower-case string Console. writeLine ("convert to lowercase English character is {0}", strEnglish. toLower (); Console. readLine () ;}} using System; using System. collections. generic; using System. linq; using System. text; using System. threading. tasks; namespace Stringchuli {class Program {static void Main (string [] args) {string strname; st Ring inputStr; string [] splitString; string joinString; string strEnglish; string email; Console. writeLine ("enter a new email address:"); email = Console. readLine (). trim (); Console. writeLine ("your email is {0}", email); // you can specify the email user name int intindex = email. indexOf ("@"); if (intindex> 0) {strname = email. substring (0, intindex); // output the mailbox user name Console. writeLine ("your username is {0}", strname);} else {Console. writeLine ("the format you entered is incorrect! ");} Console. writeLine ("enter a string and separate words with spaces:"); inputStr = Console. readLine (); Console. writeLine ("the string you entered is {0}", inputStr); // use a space to separate the string splitString = inputStr. split (''); // output the Split string Console. writeLine ("the split string is:"); foreach (string s in splitString) {Console. writeLine (s);} // The split string uses-to connect joinString = string. join ("-", splitString); // output the connected string Console. writeLine ("the connected string is {0}", joinString); Console. writeLine ("enter an uppercase English string:"); strEnglish = Console. readLine (); Console. writeLine ("your input capital string is {0}", strEnglish); // converts the input capital string to a lower-case string Console. writeLine ("convert to lowercase English character is {0}", strEnglish. toLower (); Console. readLine ();}}}
Running result: