如何修剪字符之间的空白 [英] How to trim whitespace between characters

查看:128
本文介绍了如何修剪字符之间的空白的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何删除字符之间的空格在C#?



TRIM()可用于去除空空间的字符串的开头以及末尾。例如C夏普.Trim()结果C夏普



但如何使串入 CSHARP ?我们可以用一个临时变量一起拔出使用每个循环的空间或。但是,有没有内置的方法 C#(.NET Framework 3.5中)来做到这一点像 TRIM() <? / p>

解决方案

您可以使用的与string.replace

 字符串str =C夏普 
海峡= str.Replace(,);

如果您要删除所有空格字符(空格,制表符,换行符...)

 字符串str =C夏普 
海峡= Regex.Replace(STR,@\s,);


How to remove whitespaces between characters in c#?

Trim() can be used to remove the empty spaces at the beginning of the string as well as at the end. For example " C Sharp ".Trim() results "C Sharp".

But how to make the string into CSharp? We can remove the space using a for or a for each loop along with a temporary variable. But is there any built in method in C#(.Net framework 3.5) to do this like Trim()?

解决方案

You could use String.Replace method

string str = "C Sharp";
str = str.Replace(" ", "");

or if you want to remove all whitespace characters (space, tabs, line breaks...)

string str = "C Sharp";
str = Regex.Replace(str, @"\s", "");

这篇关于如何修剪字符之间的空白的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆