[英]Remove 2 or more blank spaces from a string in c#..?
什么是從字符串中刪除 2 個或更多空格留下單個空格的有效機制。
我的意思是如果字符串是“a____b”,則 output 必須是“a_b”。
您可以使用正則表達式來替換多個空格:
s = Regex.Replace(s, " {2,}", " ");
可能像下面這樣:
var b=a.Split(new char[] {' '}, StringSplitOptions.RemoveEmptyEntries);
var noMultipleSpaces = string.Join(" ",b);
string tempo = "this is a string with spaces";
RegexOptions options = RegexOptions.None;
Regex regex = new Regex(@"[ ]{2,}", options);
tempo = regex.Replace(tempo, @" ");
您可以使用此方法 n 將您的字符串值作為參數傳遞,您還必須使用 System.Text.RegularExpressions 添加一個命名空間;
公共 static 字符串 RemoveMultipleWhiteSpace(string str)
{
// A.
// Create the Regex.
Regex r = new Regex(@"\s+");
// B.
// Remove multiple spaces.
string s3 = r.Replace(str, @" ");
return s3;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.