2013-10-03 129 views
0

如何删除“”之间的空格? 我有超过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("\" \" ", " "); 
+0

你应该给它分配回像'richTextBox1 = richTextBox1.lines.Trim()的richTextBox1' – V4Vendetta

回答

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

编辑:

missunderstood,你只是缺少分配在richTextBox1.lines.Trim()更换( “\” \ “ ”“”);

使其:

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

更换是不会改变的字符串。

+0

我的一句话可以改变,但唯一的错误是“”之间的空间,所以我怎样才能使你的代码通用。不仅字符串s – user2760129

+0

@ user2760129检查编辑答案 – wudzik

+0

得到此错误:'System.Array'不包含'Trim'的定义,并且没有扩展方法'Trim'接受类型'System.Array'的第一个参数可以找到(你是否缺少使用指令或程序集引用?) – user2760129

0

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

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

试试这个:

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