C#string class

1. String class
1. Comparison of strings
Compare(str1, str2)
str1.CompareTo(str2)
returns: int32
is less than 0: str1 is before str2 in the sort order and
equals 0: str1 and str2 appear in the sort order Same position
Greater than 0: str1 is after str2 in the sort order
For details, see:
https://docs.microsoft.com/zh-cn/dotnet/api/system.string.compare?view=net-6.0#system-string- compare(system-string-system-string)
2. String search
Contains(str): Find whether the specified character contains the string str, return bool type
IndexOf(str): Find the first occurrence of the string str, no Then return -1
LastIndexOf(str): Find the last occurrence position, if there is no position, return -1
For example:
string x="hello";
Console.WriteLine(x.IndexOf('w'));//-1
Console. WriteLine(x.IndexOf('o'));//4
3. String interception
Substring(str): All strings after the subscript in the string start from str.
Substring(str, Len): The subscript in the string is a string of Len length after the subscript starts from str.
For example:
string x="hello";
Console.WriteLine(x.Substring(2));//llo
Console.WriteLine(x.Substring(2, 2));//ll
4. String splitting
Split(str ): Split the string by str, and its return value is a string array.
For example:

string d = "锄禾日当午#汗滴禾下土#谁知盘中餐#粒粒皆辛苦";
string[] dd = d.Split('#');
for (int i = 0; i < dd.Length; i++)
  {
    
    
   Console.WriteLine(dd[i]);
   /* 锄禾日当午
    * 汗滴禾下土
    * 谁知盘中餐
    * 粒粒皆辛苦
    */
  }

5. Combination of strings
string.Concat(str1, str2, …., strn): Concatenate n strings
string xx = “aaa”, yy = “bbb”;
Console.WriteLine(xx + yy);
Console. WriteLine(string.Concat(xx, yy));//This is highly efficient
6. String deletion
Trim(): Delete the spaces at the beginning and end of the string
For example:
string z = " cc dd ";
Console.WriteLine (z.Trim());//"cc dd"
7. String replacement
Replace(oldStr, newStr): Use newStr to replace oldStr in the string
For example:
string z = "cc dd ";
Console.WriteLine( z.Replace("cc", "CC"));//" CC dd "
8. String insertion
Insert(index, str): index is the position to be inserted, str is the character to be inserted
For example:
string z = " cc dd ";
Console.WriteLine(z.Insert(2, "ww"));//" wwcc dd "
9. String case conversion
ToLower lowercase, ToUpper uppercase
string x=“Hello”;
Console.WriteLine(x.ToLower());//hello
Console.WriteLine(x.ToUpper());//HELLO

2. String exercises
1. The user enters an email account and extracts the user name and domain name (@ does not belong to any one)

Console.WriteLine("输入邮箱:");
string email = Console.ReadLine();
string[] e = email.Split('@');
for (int i = 0; i < e.Length; i++)
{
    
    
 Console.WriteLine(e[i]);
}

2. The user enters a sentence and queries the position of e in it

Console.WriteLine("输入一句话(判断e的位置):");
string words = Console.ReadLine();
int a = words.IndexOf('e');
if (a == -1)
{
    
    
 Console.WriteLine("该句话没有e");
}
else
{
    
    
 Console.WriteLine("e的位置在第:"+(a+1)+"个");
}

3. Enter a string, if there is evil in the string, replace it with **

Console.WriteLine("输入一句话 :(出现邪恶则换为**)");
string b = Console.ReadLine();
Console.WriteLine(b.Replace("邪恶","**"));

4. Change the elements in the array {"Lin Yang", "Yu Zhouzhou", "Yanmo", "Zhao Qiaoyi"} into Lin Yang|Yu Zhouzhou|Yanmo|Zhao Qiaoyi

string[] name = {
    
     "林阳", "余周周", "炎默", "赵乔一" };
for(int i = 0; i < name.Length; i++)
{
    
    
 if (i < (name.Length - 1))
  {
    
    
   Console.Write(name[i].Insert(name[i].Length, "|"));
  }
 else
  {
    
    
   Console.WriteLine(name[i]);
  }
}

5. Turn "hoeing day at noon#汗混下土#吟知盘中食#Every particle is hard work" into the
day of hoeing,
sweat dripping down the soil,
who knows that every particle of Chinese food
is hard work.

string s1 = "锄禾日当午#汗滴禾下土#谁知盘中餐#粒粒皆辛苦";
string[] s2 = s1.Split('#');
for (int i = 0; i < s2.Length; i++)
{
    
    
 if (i == s2.Length - 1)
  {
    
    
   Console.WriteLine(s2[i].Insert(s2[i].Length,"。"));
  }
  else
  {
    
    
    Console.WriteLine(s2[i].Insert(s2[i].Length+1, ","));
  }
}

Guess you like

Origin blog.csdn.net/weixin_44706943/article/details/126089045