C#—字符串大小写转换、判断字符串长度、移除字符串中的内容

如何判断一个字符串长度:

  • .Length 获取长度
Console.WriteLine("请您输入家庭住址"); //提示输入内容
string name = Console.ReadLine();  //从控制台读取输入内容,给name变量

Console.WriteLine(name.Length);  //控制台输出字符串长度
Console.ReadKey();

字符串大写、小写转换

  • .ToLower 把字符串转换成小写
  • .ToUpper 把字符串转换成大写
  • .Equals 比较两个字符串是否相同
Console.WriteLine("请输入科目一课程名称"); //提示输入
string shu = Console.ReadLine(); //读输入的内容
shu = shu.ToLower();  ///把字符串转换成小写的
shu = shu.ToUpper();  ///把字符串转换成大写的

Console.WriteLine("请输入科目二课程名称");
string shu1 = Console.ReadLine();  //提示输入
shu1 = shu1.ToLower();  ///把字符串转换成小写的
shu1 = shu1.ToUpper();  ///把字符串转换成大写的

bool result = shu.Equals(shu1, StringComparison.OrdinalIgnoreCase);  // Equals 比较连个字符串是否相同
if (result)
{
     Console.WriteLine("课程相同");
}
else
{
     Console.WriteLine("课程不相同");
}

if (shu == shu1)
{
     Console.WriteLine("课程相等" + shu);
}
else
{
     Console.WriteLine("课程不相等{0}---------{1}", shu, shu1);
}
Console.ReadKey();

如何移除字符串中不想要的内容

  • Split 对字符进行切割
  • StringSplitOptions.RemoveEmptyEntries:返回的数组值不包含空字符串的数组元素
string str = "哈  哈  ----,我   又  ----长  高  了";  //定义字符串变量
char[] chs = new char[] { ' ', '-' };  //数组里边存入要去除的内容
string[] result = str.Split(chs, StringSplitOptions.RemoveEmptyEntries);  //已经把不想要的给切掉了

for (int i = 0; i < result.Length; i++) //for 循环
{
    Console.Write(result[i]); //输出内容
}
Console.ReadKey();

输出结果:哈哈,我又长高了
发布了63 篇原创文章 · 获赞 9 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/weixin_44031029/article/details/104432208