移除Java字符串中选定的换行符并保持字符串顺序。

huangapple go评论76阅读模式
英文:

Remove selected new line in java string and maintain the String order

问题

I want to remove the new line from the given string format

This is test;

This is new test;

This is new test2;

This is another string test;

this is more space string test;

Output should be like this:

This is test;
This is new test;
This is new test2;
This is another string test;
this is more space string test;

I know I can use regex expression or replace all with "\n" but in that case, all the string will be replaced in single line and the order of string which i want won't be maintained.

英文:

I want to remove the new line from the given string format

     This is test;
 
 
 
 This is new test;
 
 
 
 
 This is new test2;
 
 This is another string test;
 
 
 
 
 
 
 
 
 this is more space string test; 

Output should be like this:

 This is test;
 This is new test;
 This is new test2;
 This is another string test;
 this is more space string test; 

I know I can use regex expression or replace all with "\n"
but in that case, all the string will be replaced in single line and the order of string which i want won't be maintained.?

答案1

得分: 2

这部分内容的中文翻译如下:

一种选择是在点模式下对以下模式进行正则替换:

\r?\n\s*

然后,只需替换为一个换行符,以保留原始的单个换行符。

String input = "Line 1\r\n\n\n\n     Line 2 blah blah blah\r\n\r\n\n   Line 3 the end.";
System.out.println("Before:\n" + input + "\n");
input = input.replaceAll("(?s)\r?\n\\s*", "\n");
System.out.println("After:\n" + input);

这将打印出:

Before:
Line 1



     Line 2 blah blah blah


       Line 3 the end.

After:
Line 1
Line 2 blah blah blah
Line 3 the end.
英文:

One option would be to do a regex replacement on the following pattern, in dot all mode:

\r?\n\s*

Then, just replace with a newline, to retain an original single newline.

String input = "Line 1\r\n\n\n\n     Line 2 blah blah blah\r\n\r\n\n   Line 3 the end.";
System.out.println("Before:\n" + input + "\n");
input = input.replaceAll("(?s)\r?\n\\s*", "\n");
System.out.println("After:\n" + input);

This prints:

Before:
Line 1



     Line 2 blah blah blah


       Line 3 the end.

After:
Line 1
Line 2 blah blah blah
Line 3 the end.

答案2

得分: 0

项目结构

input.txt

这是测试;

这是新测试;

这是新测试2;

这是另一个字符串测试;

这是更多空格字符串测试;

Main.clas

public class Main {
private static String input = "";
public static void main(String[] args) throws IOException {
// 读取文件,考虑所有空格和边距。
Files.lines(Paths.get("src/input.txt"), StandardCharsets.UTF_8).forEach(e->{
input = input.concat(e);
input = input.concat("\n");
});

    while (input.contains("\n\n")){
        input = input.replace("\n\n","\n");
    }
    // 修剪两侧的边距
    input = input.trim();
    System.out.println(input);
}

}

结果

这是测试;
这是新测试;
这是新测试2;
这是另一个字符串测试;
这是更多空格字符串测试;

英文:

Project structure

input.txt

       This is test;



 This is new test;




 This is new test2;

 This is another string test;








 this is more space string test;

Main.clas

public class Main {
   private static String input = "";
   public static void main(String[] args) throws IOException {
       //Reading the file, taking into account all spaces and margins.
        Files.lines(Paths.get("src/input.txt"), StandardCharsets.UTF_8).forEach(e->{
           input = input.concat(e);
           input = input.concat("\n");
        });

        while (input.contains("\n\n")){
            input = input.replace("\n\n","\n");
        }
        //trim the margins on both sides
        input = input.trim();
        System.out.println(input);
    }
}

Result

This is test;
This is new test;
This is new test2;
This is another string test;
this is more space string test;

答案3

得分: 0

尝试这个。

String text = Files.lines(Paths.get("input.txt"))
    .map(line -> line.trim())
    .filter(line -> !line.isEmpty())
    .collect(Collectors.joining("\n"));
System.out.println(text);

输出

这是测试;
这是新的测试;
这是新的测试2;
这是另一个字符串测试;
这是更多空格的字符串测试;
英文:

Try this.

String text = Files.lines(Paths.get("input.txt"))
    .map(line -> line.trim())
    .filter(line -> !line.isEmpty())
    .collect(Collectors.joining("\n"));
System.out.println(text);

output

This is test;
This is new test;
This is new test2;
This is another string test;
this is more space string test;

huangapple
  • 本文由 发表于 2020年8月13日 17:56:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/63392592.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定