首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >弦修剪移除空间?

弦修剪移除空间?
EN

Stack Overflow用户
提问于 2013-10-03 06:43:12
回答 3查看 289关注 0票数 0

如何删除“”之间的空白?我有100多行丰富的文本框,我的句子在“”之间的空白处如下所示。

我的句子

代码语言:javascript
复制
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
.
.
.

应该是:

代码语言:javascript
复制
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
.
.
.

我的代码

代码语言:javascript
复制
richTextBox1.lines.Trim().Replace("\"  \" ", " ");
EN

回答 3

Stack Overflow用户

发布于 2013-10-03 06:46:05

使用regex:

代码语言:javascript
复制
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().Replace(“\”\“”、“”)中缺少赋值;

使:

代码语言:javascript
复制
richTextBox1.lines = richTextBox1.lines.Trim().Replace("\"  \" ", " ");

替换不是在更改字符串。

票数 1
EN

Stack Overflow用户

发布于 2013-10-03 06:50:43

你错过了重新分配到richTextBox1的机会。替换()用正确的文本返回字符串值。您的代码应该是:

代码语言:javascript
复制
for(int i = 0; i < richTextBox1.Lines.Count(); i++)
{
    richTextBox1.Lines[i] = richTextBox1.Lines[i].Trim().Replace("\" \" ", " ");
}
票数 0
EN

Stack Overflow用户

发布于 2013-10-03 07:37:00

试试这个:

代码语言:javascript
复制
richTextBox1 = richTextBox1.lines.Trim().Replace(" \" ", " \"");
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19152477

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档