簡體   English   中英

如何從文本中刪除新行和制表符?

[英]How to remove new lines and tabs from text?

我有一個像

"this is line 1\n\t\n\t\tthis is line 2\n\n\n\t\tthis is line 3\t\t\tthis is line 4"

我想要做的是從文本中刪除重復的特定字符(“\\n”、“\\t”)。

預期結果;

"this is line 1\n\tthis is line 2\n\tthis is line 3\tthis is line 4"

我有下面的正則表達式,但它只刪除重復的字符。

String text = text.replaceAll("([\n\t])\\1+", "$1");

是否有任何正則表達式?

編輯:例如有一個像

"\n\t\tHELLOWORLD\t\t\n\n\n\t"

我想得到的是;

"\n\tHELLOWORLD\t\n"

這是解決方案;

首先使用第一個正則表達式刪除所有重復字符。 之后替換連續的文本。

for(int i = 0; i < 10; i++){
    text= text.replaceAll("([\n\t])\\1+", "$1");
    text= text.replaceAll("\n\t\n", "\n\t");
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM