简体   繁体   English

Java缩写后用String中的空格替换新行

[英]Java replace new line with space in String after abbreviations

Whenever I find an abbreviation within a sentence (like Mr., prf. and so on) I would like to delete the '\\n' character at the end of each sentence that contains an abbreviated word. 每当我在句子中找到缩写词(例如Mr.,prf。等)时,我都想删除每个包含缩写词的句子末尾的'\\ n'字符。 Any ideas are welcome. 任何想法都欢迎。 INPUT OUTPUT 输入输出

My idea so far is: 到目前为止,我的想法是:

List<String> pres = Arrays.asList("dl","Dl", "Prf", "Ing");
for(int i=1;i<4;i++){
    if (z.contains(pres.get(i)))
        f=z.indexOf(pres.get(i));
    z.replaceFirst("\\n"," ");//how i can use my f here to get rid of next new line...?
}

Here is an approximate solution, without knowing the full list of abbreviations which you want to check. 这是一个近似的解决方案,不知道您要检查的缩写的完整列表。 You may search on the following pattern, and replace with the first capture group: 您可以搜索以下模式,并替换为第一个捕获组:

((?:Mr|Mrs|Dr)\.[^.]+\.)\n

This will identify the last abbreviation in any sentence which ends in dot immediately followed by a \\n newline. 这将标识出所有以点结尾并紧跟\\n换行符的句子中的最后一个缩写。 Note that in cases with more than one abbrevation in a single sentence, it would only match the last abbreviation. 请注意,如果单个句子中有多个缩写,则只能匹配最后一个缩写。

String input = "Here is a sentence.  Said Mrs. Canopoy, here is another sentence about Mr. Potato Head.\r\nHere is a third sentence.";
System.out.println(input);
input = input.replaceAll("((?:Mr|Mrs|Dr)\\.[^.]+\\.)\\r\\n", "$1");
System.out.println(input);

Demo 演示

I only check for Mr. , Mrs. , or Dr. , but you may add as many abbreviations as you want to the alternation. 我只检查Mr.Mrs. .或Dr. ,但是您可以根据需要添加任意多个缩写。

Just use this: 只需使用此:

String s = "Mike and Mr.\nDave take dinner.\nThat is very important.\nMe and Ing.\nMike bla bla..";
s = s.replaceAll("(Mr.|Ing.)\n", "$1 ");

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

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