---------------------- ASP.Net+Unity开发、.Net培训、期待与您交流! ----------------------
static void Main(string[] args)
{
//方法一
string hello = " hello world,你 好 世界 ! ";//原字符串
string[] strs = (hello.Trim()).Split(' ');
List<string> strList = new List<string>();
foreach (string str in strs)
{
if (str != "")
{
strList.Add(str);
}
}
Console.WriteLine(string.Join(" ", strList));
//方法二
string he = " hello world,你 好 世界 ! ";
Regex regex = new Regex(@"( )+");
Console.WriteLine(regex.Replace(he.Trim()," "));
Console.ReadKey();
}
本文介绍了两种使用C#进行字符串处理的方法:一种是通过遍历并利用List存储非空字符串片段;另一种是采用正则表达式快速移除指定字符。这两种方法均能有效去除字符串中的特定字符。

1068

被折叠的 条评论
为什么被折叠?



