仅删除单个字符之间的空格

时间:2014-03-10 03:20:34

标签: c# regex string

我有以下字符串,我正在尝试删除仅单个字符之间的空格,从而使WORDS保持适当的间隔。

示例字符串

This is my s e n t e n c e.

结果必须是:

This is my sentence.

另一个例子:

Words must remain properly spaced but **s i n g l e** characters **l i k e** this are joined.

会变成:

Words must remain properly spaced but **single** characters **like** this are joined.

2 个答案:

答案 0 :(得分:3)

res = Regex.Replace(s, @"(?<=\b\w)\s(?=\w\b)", "");

答案 1 :(得分:-1)

不确定这是否正是您正在寻找的但是试试这个。

string value = "w o r d";
char[] sentence = value.ToCharArray();

for (int i = 0; i < value.length+1; i++) {
    if (sentence[++i] = " " && sentence [i] != " ") {
        sentence [i] = sentence [++i];
    }
}