C#中字符串的处理
在C#中,有时候我们会对一个声明的字符串进行一些处理,比如说将字符串中的英文字符进行大小写转换,将字符串前后的或者中间的空字符去掉,或者是 将字符串中的一些同类型的字符去掉等等,接下来,让我们一起学习吧!
- ToLower:将字符串中的英文字母全部准换成小写
- ToUpper:将字符串中的英文字母全部转换成大写
- Trim :将字符串前后的空白字符删除;其中 TrimStart是将字符串开头的空白字符删除;TrimEnd是将字符串后面的空白字符删除
- Split :是将规定的字符删除,其中Split返回的值同样也是一个数组
例子:
①.对于ToLower的使用:
代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _zifuchuan
{
class Program
{
static void Main(string[] args)
{
string str = "I'm a student! I'm a good man!";
string res = str.ToLower();
Console.WriteLine( res);
}
}
}
②.对于ToUpper的使用:
程序:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _zifuchuan
{
class Program
{
static void Main(string[] args)
{
string str = "I'm a student! I'm a good man!";
string res = str.ToUpper();
Console.WriteLine( res);
}
}
}
③.对于Trim的使用:
程序:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _zifuchuan
{
class Program
{
static void Main(string[] args)
{
string str = " I'm a student! I'm a good man! ";
string res = str.Trim();
Console.WriteLine( str + "|");
Console.WriteLine( res);
}
}
}
其中对于TrimStart和TrimEnd这两个。在此不在举例了,有兴趣的可以自己敲一下。
④:对于Spilt的使用:
程序:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _zifuchuan
{
class Program
{
static void Main(string[] args)
{
string str = " I'm a student! I'm a good man! ";
string[] res = str.Split('!');// split返回的值为一个字符串数组,在这里直接将str数组中的!删除,只打印其他内容
Console.WriteLine( str + "|");
foreach (string i in res)// 遍历数组
{
Console.WriteLine(i);
}
}
}
}