[英]Remove The Space in a String in c#
我想知道如何刪除字符串中的空格。 例如取一個字符串a= "Hello World"
。 一旦找到空格,則"Hello"
和"World"
應該分開並存儲在單獨的字符串中。 像b="Hello"
和c="World"
。 這是可能的。 誰能幫我。
提前致謝。
做一個
var words = a.Split(' ');
這將返回一個包含每個單詞的數組
foreach(var word in words)
{
Trace.WriteLine(word);
}
只需使用內置拆分功能:
string a= "Hello World"
var words = a.Split(' ');
使用string.Split :
string[] words = "Hello World".Split(' ');
如果性能很重要,您可以使用此處這樣的代碼勝過String.Split()
。
string cname = "Hong Kong";
cname = Regex.Replace(cname, @"\s", "-");
Response.Write(cname);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.