c#字符串去掉空格的二种方法(去掉两端空格)
更新时间:2014年02月20日 10:07:59 作者:
本文主要介绍了字符串去掉两端空格,并且将字符串中多个空格替换成一个空格的方法,需要的朋友可以参考下
使用字符串的方法:
trim();去掉字符串两端空格
split();切割
string.join();连接
复制代码 代码如下:
class Program
{
static void Main(string[] args)
{
//原字符串
string str = " hello world,你 好 世界 ! ";
//去掉两端空格
str= str.Trim();
//以空格切割
string [] strArray= str.Split(new char[]{' '}, StringSplitOptions.RemoveEmptyEntries);
//以空格连接
string newStr= string.Join(" ", strArray);
Console.WriteLine(newStr);
Console.ReadKey();
}
}
使用正则的方法:
复制代码 代码如下:
class Program
{
static void Main(string[] args)
{
//原字符串
string str = " hello world,你 好 世界 ! ";
string s = Regex.Replace(str, @"\s+", " ").Trim();
Console.WriteLine(s);
Console.ReadKey();
}
}
相关文章
C#.net编程创建Access文件和Excel文件的方法详解
这篇文章主要介绍了C#.net编程创建Access文件和Excel文件的方法,结合实例形式总结分析了C#创建Access与Excel文件的几种常用技巧,具有一定参考借鉴价值,需要的朋友可以参考下2016-06-06


最新评论