- 1.将字符串转为大写或小写-----ToUpper & ToLower
- 2.判断两个字符串是否相等(不区分大小写)---Equals
- 3.字符串的分割---Split
- 4.判断字符串是否包含某个字符串---Contains & 替换字符串---Replace
- 5.根据索引、字符个数截取字符串---Substring
- 6.获取字符串中第一次、最后一次出现某个字符的索引---IndexOf & LastIndexOf
- 7.去掉字符串开头、结尾的空字符串---Trim、TrimStart、TrimEnd
- 8.判断字符串是否为空或null---string.IsNullOrEmpty
- 9.根据数组中的元素,组合新的字符串
- 10.判断字符串是否以某个字符串开头、结尾---StartsWith & EndsWith
1.将字符串转为大写或小写-----ToUpper & ToLower
string say = "hellworld"; say.ToUpper(); Console.WriteLine(say); say = say.ToUpper(); Console.WriteLine(say); Console.ReadKey(); // hellworld // HELLWORLD
string say = "HELLOWORLD"; say.ToLower(); Console.WriteLine(say); say = say.ToLower(); Console.WriteLine(say); Console.ReadKey(); // HELLOWORLD // helloworld
2.判断两个字符串是否相等(不区分大小写)—Equals
string say = "HELLOWORLD"; string said = "helloworld"; bool result = say.Equals(said, StringComparison.OrdinalIgnoreCase); Console.WriteLine(result); Console.ReadKey(); // True
3.字符串的分割—Split
string say = "a b c _ , f, , , _,_m ";
char[] chs = {' ', '_', ',' };
string[] result = say.Split(chs, StringSplitOptions.RemoveEmptyEntries);
foreach(string s in result)
{
Console.WriteLine(s);
}
Console.ReadKey();
//a
//b
//c
//f
//m
4.判断字符串是否包含某个字符串—Contains & 替换字符串—Replace
string str = "hello world alien";
if (str.Contains("alien"))
{
str = str.Replace("alien", "Alien");
}
Console.WriteLine(str);
Console.ReadKey();
// hello world Alien
5.根据索引、字符个数截取字符串—Substring
string str = "hello world alien"; str = str.Substring(1, 9); // 从索引1一直到索引9,[1, 9] , 第一个索引是0 Console.WriteLine(str); Console.ReadKey(); // ello worl
6.获取字符串中第一次、最后一次出现某个字符的索引—IndexOf & LastIndexOf
string path = @"C:abcdealien.cs";
//int index = path.IndexOf("\"); // 获取某字符串第一次出现的索引
int index = path.LastIndexOf("\"); // 获取某字符串最后一次出现的索引
Console.WriteLine(index);
string file_name = path.Substring(index + 1);
Console.WriteLine(file_name);
Console.ReadKey();
// 12
// alien.cs
7.去掉字符串开头、结尾的空字符串—Trim、TrimStart、TrimEnd
string str = " alien "; string str_1 = str.Trim(); Console.WriteLine(str_1); string str_2 = str.TrimStart(); // 去掉开头的空字符串 Console.WriteLine(str_2); string str_3 = str.TrimEnd(); // 去掉尾部的空字符串 Console.WriteLine(str_3); Console.ReadKey(); //alien //alien // 此处其实包含alien后面的空字符串 // alien
8.判断字符串是否为空或null—string.IsNullOrEmpty
string str = "alien";
if (string.IsNullOrEmpty(str))
{
Console.WriteLine("空 或 null");
}
else
{
Console.WriteLine("非空或null");
}
// 非空或null
9.根据数组中的元素,组合新的字符串
string[] names = { "hello", "wolrd", "alien" };
string strNew = string.Join("_", names);
Console.WriteLine(strNew);
Console.ReadKey();
// hello_wolrd_alien
10.判断字符串是否以某个字符串开头、结尾—StartsWith & EndsWith
string names = "helloworldalien";
bool result_1 = names.StartsWith("hello");
Console.WriteLine(result_1);
bool result_2 = names.EndsWith("alien");
Console.WriteLine(result_2);
Console.ReadKey();
// True
// True



