手机
当前位置:查字典教程网 >编程开发 >C#教程 >c#字符串去掉空格的二种方法(去掉两端空格)
c#字符串去掉空格的二种方法(去掉两端空格)
摘要:使用字符串的方法:trim();去掉字符串两端空格split();切割string.join();连接复制代码代码如下:classProgr...

使用字符串的方法:

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#字符串去掉空格的二种方法(去掉两端空格)】相关文章:

C# 改变无边框窗体尺寸大小的方法

c#中SAPI使用总结——SpVoice的使用方法

C#中使用split分割字符串的几种方法小结

解析C#中@符号的几种使用方法详解

c#创建Graphics对象的三种方法

c# 匿名方法的小例子

C#中将字符串转换为整型的三种解决方法总结

C#中实现任意List的全组合算法代码

用C#实现启动另一程序的方法实例

C# 禁用鼠标中间键的方法

精品推荐
分类导航