一、String.Trim() ,TrimStart(),TrimEnd() 去掉字符串?前后?的空格
string str = " Hello World !\n Hello World ! ";
str.Trim()
二、String.Replace()函數(shù)可以去掉字符串中?所有?的空格
string str = " Hello World !\n Hello World ! ";
str.Replace(" ", "")
三、正則表達(dá)式方法Regex.Replace()和匹配符\s(匹配任何空白字符,包括空格,制表符,換頁符等,與[\f\n\t\r\v]等效)
//使用正則去除空格,換行,制表符,換頁符
Regex regex =new Regex("\\s+",RegexOptions.IgnoreCase);
string result = regex.Replace(str,"");
Console.WriteLine(result);
案例說明:
public static void Test2()
{
string str = " Hello World !\n Hello World ! ";
Console.WriteLine(str.Trim());
Console.WriteLine(str.Replace(" ", ""));
//使用正則去除空格,換行,制表符,換頁符
Regex regex = new Regex("\\s+", RegexOptions.IgnoreCase);
string result = regex.Replace(str, "");
Console.WriteLine(result);
}
?
四、完善中.....文章來源:http://www.zghlxwxcb.cn/news/detail-623282.html
更多:文章來源地址http://www.zghlxwxcb.cn/news/detail-623282.html
.Net內(nèi)存管理基礎(chǔ)知識
C#時(shí)間使用整理,DateTime 使用整理
C# 合并對象_C# 對象合并整理
到了這里,關(guān)于C# 去除字符串中的空格方案整理的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!