字符串修剪删除空间?

时间:2013-10-03 06:43:12

标签: c# string trim

如何删除“”之间的空格? 我有超过100行富文本框,我的句子如下所示,“”之间有空格。

我的句子

remove only " railing" spaces of a string in Java
remove only " trailing" spaces of a string in Java
remove only " ling" spaces of a string in Java
remove only " ing" spaces of a string in Java
.
.
.

应该是:

remove only "railing" spaces of a string in Java
remove only "trailing" spaces of a string in Java
remove only "ling" spaces of a string in Java
remove only "ing" spaces of a string in Java
.
.
.

我的代码

richTextBox1.lines.Trim().Replace("\"  \" ", " ");

3 个答案:

答案 0 :(得分:1)

使用正则表达式:

string RemoveBetween(string s, char begin, char end)
{
    Regex regex = new Regex(string.Format("\\{0}.*?\\{1}", begin, end));
    return regex.Replace(s, string.Empty);
}

string s = "remove only \"railing\" spaces of a string in Java";
s = RemoveBetween(s, '"', '"');

来源:https://stackoverflow.com/a/1359521/1714342

您可以定义要删除字符串的字符。详细了解Regex.Replace

编辑:

误解了,你只是缺少在richTextBox1.lines.Trim()中的赋值。替换(“\”\“”,“”);

制作:

richTextBox1.lines = richTextBox1.lines.Trim().Replace("\"  \" ", " ");

替换不会改变字符串。

答案 1 :(得分:0)

您错过了对richTextBox1的重新分配。 Replace()返回正确文本的字符串值。 你的代码应该是:

for(int i = 0; i < richTextBox1.Lines.Count(); i++)
{
    richTextBox1.Lines[i] = richTextBox1.Lines[i].Trim().Replace("\" \" ", " ");
}

答案 2 :(得分:0)

试试这个:

richTextBox1 = richTextBox1.lines.Trim().Replace(" \" ", " \"");
相关问题