[英]How to remove a a space given between charcters/Words in Textbox
我有一個函數可以刪除文本框中的多個空格,並使字符/單詞帶有一個空格。我需要刪除所有空格並將其作為單個文本,是否有辦法做到這一點。
我用來刪除Space的函數是
public static string Removespace(string inputString)
{
System.Text.StringBuilder strbuilder = new System.Text.StringBuilder();
string[] spacechars = inputString.Split(new char[] { ' ', '\n', '\t', '\r', '\f', '\v' }, StringSplitOptions.RemoveEmptyEntries);
int size = parts.Length;
for (int i = 0; i < size; i++)
strbuilder.AppendFormat("{0} ", spacechars[i]);
return strbuilder.ToString();
}
嘗試使用String.Replace
方法。
string mystring = "This is test";
mystring = mystring.Replace(" ","");
或者您也可以使用正則表達式
mystring = Regex.Replace(mystring, @"\s+", "");
替換所有空格字符:
string s= "This is a test";
string s2 = new String(s.Where(c => !Char.IsWhiteSpace(c)).ToArray());
Where(c => !Char.IsWhiteSpace(c)
部分過濾掉各種空白字符,結果放入數組中,該數組用於構建新字符串。
嘗試這個
string _myString= "Apple Bat Cat";
myString= myString.Replace(" ", "");
輸出: AppleBatCat
還看看字符串替換方法MSDN
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.