您的位置:首页 > 其它

将字符串 “ hello word,你 好 世 界 ! ” 两端空格去掉并且将其中的其他所有空格替换成一个空格 输出结果为“hello word,你 好 世界”

2018-08-14 16:04 519 查看
string str = "     hello      word,你 好 世  界  !     ";
string msg = str.Trim();  //去掉首尾空格
//使用split分割字符串,stringSplitOptions枚举去掉空格返回字符串数组类型
string[] s = msg.Split(new char[] { ' ' },StringSplitOptions.RemoveEmptyEntries);
//for (int i = 0; i < s.Length; i++)
//{
//    Console.WriteLine(s[i]);
//}
//在使用字符串join连接参数,它string数据s 用空格连接起来
string m= string.Join(" ", s);
Console.WriteLine(m);
Console.ReadKey();

 

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐