簡體   English   中英

弦裝飾是否可以去除空間?

[英]string trim remove space?

如何刪除“”之間的空白? 我有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("\"  \" ", " ");

使用正則表達式:

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(“ \\” \\“”,“”);中缺少分配

做了:

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

替換不更改字符串。

您缺少對RichTextBox1的重新分配。 Replace()返回帶有正確文本的字符串值。 您的代碼應為:

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