有没有办法从字符串中删除每个特殊字符,如:
"\r\n 1802 S St Nw<br>\r\n Washington, DC 20009"
Run Code Online (Sandbox Code Playgroud)
并写它像:
"1802 S St Nw, Washington, DC 20009"
Run Code Online (Sandbox Code Playgroud)
要删除特殊字符:
public static string ClearSpecialChars(this string input)
{
foreach (var ch in new[] { "\r", "\n", "<br>", etc })
{
input = input.Replace(ch, String.Empty);
}
return input;
}
Run Code Online (Sandbox Code Playgroud)
用单个空格替换所有双倍空间:
public static string ClearDoubleSpaces(this string input)
{
while (input.Contains(" ")) // double
{
input = input.Replace(" ", " "); // with single
}
return input;
}
Run Code Online (Sandbox Code Playgroud)
您也可以将两种方法分成一个方法:
public static string Clear(this string input)
{
return input
.ClearSpecialChars()
.ClearDoubleSpaces()
.Trim();
}
Run Code Online (Sandbox Code Playgroud)