从字符串的一部分中删除空格

时间:2012-05-11 15:06:00

标签: c#

我有文字:

SMS \r\n\t•    Map - locations of

如何删除•和第一个后续字符之间的所有空格?

以上示例应该导致

SMS \r\n\t•Map - locations of

3 个答案:

答案 0 :(得分:3)

通过使用正则表达式,可以这样做:

var input = "SMS \r\n\t•    Map - locations of";

var regexPattern = @"(?<=•)\s+(?=\w)";
var cleanedInput = Regex.Replace(input, regexPattern, String.Empty);

这将替换•和第一个单词字符之间的空白字符串。

答案 1 :(得分:1)

string s = "SMS \r\n\t•    Map - locations of";
string[] temp = s.Split('•');
s = temp[0]+temp[1].TrimStart(' ');

答案 2 :(得分:0)

您可以使用此正则表达式:

string toInsertBetween = string.Empty;
string toReplace = "SMS \r\n\t•    Map - locations of";
string res = Regex.Replace(toReplace, "•[ ]+([^ ])", "•" + toInsertBetween + "$1");
相关问题