在C#中,可以使用Replace()方法去掉字符串中的所有空格。下面是一個示例代碼:
Replace()
string str = "Hello World"; string newStr = str.Replace(" ", ""); // 去掉所有空格 Console.WriteLine(newStr); // 輸出:HelloWorld
在這個示例中,Replace()方法用來將字符串中的空格替換為空字符串,從而去掉所有空格。