简体   繁体   English

弦装饰是否可以去除空间?

[英]string trim remove space?

How can I remove empty space between " " ?. 如何删除“”之间的空白? I have more than 100 lines of rich text box and my sentence goes like below with empty space between " ". 我有100多行富文本框,句子如下所示,“”之间留有空白。

my sentence 我的句子

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
.
.
.

should be: 应该:

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
.
.
.

My code 我的密码

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

Using regex: 使用正则表达式:

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, '"', '"');

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

You can define between which characters you wish to remove string. 您可以定义要删除字符串的字符。 Read more about Regex.Replace 阅读有关Regex.Replace更多信息

EDIT: 编辑:

missunderstood, you are just missing assign in richTextBox1.lines.Trim().Replace("\\" \\" ", " "); 误解了,您只是在richTextBox1.lines.Trim()。Replace(“ \\” \\“”,“”);中缺少分配

make it: 做了:

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

Replace is not changing string. 替换不更改字符串。

You're missing the reassign to richTextBox1. 您缺少对RichTextBox1的重新分配。 Replace() returns string value with correct text. Replace()返回带有正确文本的字符串值。 Your code should be: 您的代码应为:

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

尝试这个:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM