C# basics ⑨-string (the difference between string str = null and string str = "")

One, string

ToLower, Toupper: uppercase and lowercase letters conversion

str=str.ToLower(): get the lowercase form of the string

str=str.Toupper(): get the uppercase form of the string

sq.Equals(s2,StringComparison.OrdinallgnoreCase): Compare two strings with case-insensitive comparison str.Split: Cut the string

Replacement string (Replace)

Determine whether this string contains this string (Contains)

Substring: intercepted character string (Note: the intercepted length cannot exceed the character string)

Substring: find a string index and then intercept

StartsWith, EndsWith: Determine whether to start and end with a string

IndexOf: find the index of the string

LastIndexOf: find the last index position

Insert: Insert a string at an index

forr: string output backwards

forr: string output in reverse order

str.Split: extract strings separately

string.Join cuts the string and adds a spacer

IsNullOrEmpty whether there are null characters

Remove(3): Remove from the third position (only keep the first three)

Remove(3,2): start from the third position

Trim(): delete the space part


2. Actual combat drills

Output string length

string str = "今天是个好天气";
Console.WriteLine(str.Length);      //在控制台显示

String to character array (loop output)

字符串变字符数组(循环输出)
string str = "今天是个好天气";

char[] chr = new char[str.Length];
for (int i = 0; i < str.Length ; i++)
{
    chr[i] = str[i];
}

for (int i = 0; i < chr.Length; i++)
{
    Console.WriteLine(chr[i]);
}
Console.ReadKey();

String array to string (string array becomes new string array)

string str = "今天是个好天气";
char[] chr=str.ToCharArray();   //变成字符数组
chr[3] = '真';     //赋值
string str1 = new string(chr);   //变成新的字符串
Console.WriteLine(str1);
Console.ReadKey();
 //输出结果:今天是真好天气

Randomly enter a name you think of, and then output its string length lenght: you can get the length of the string

Console.WriteLine("随机输入你心中想到的一个名字");
string str = console.readline();
Console.WriteLine(str.Length );

Two students enter the name of their favorite course to determine whether they are the same. If they are equal, output the same course that you both like.

  If they are not the same, output the courses that you two like differently

Console.WriteLine("输入第一门喜欢的课程名称");    //输入第一门课程
string lesson1 = Console.ReadLine();            //在控制台接收用户输入内容
            
Console.WriteLine("输入第二门喜欢的课程名称");    //输入第二门课程
string lesson2 = Console.ReadLine();            //在控制台接收用户输入内容
           
if (lesson1 == lesson2)                         //如果第一门课程=第二门课程
{
   Console.WriteLine("课程一样" + lesson1);     //两门课程相同,输出
}
else
{
    Console.WriteLine("课程不同,分别是:{0},{1}", lesson1, lesson2);     //两门课程不同,输出

3. The difference between string str = null and string str = ""

String str = null does not allocate memory space to it , while string str=\"\" allocates memory space of an empty string to it. string str = null no string object

string str = ""There is a string object

Guess you like

Origin blog.csdn.net/weixin_43319713/article/details/109551427