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

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

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

  1. This is test;
  2. This is new test;
  3. This is new test2;
  4. This is another string test;
  5. this is more space string test;

Output should be like this:

  1. This is test;
  2. This is new test;
  3. This is new test2;
  4. This is another string test;
  5. 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*

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

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

这将打印出:

  1. Before:
  2. Line 1
  3. Line 2 blah blah blah
  4. Line 3 the end.
  5. After:
  6. Line 1
  7. Line 2 blah blah blah
  8. Line 3 the end.
英文:

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

  1. \r?\n\s*

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

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

This prints:

  1. Before:
  2. Line 1
  3. Line 2 blah blah blah
  4. Line 3 the end.
  5. After:
  6. Line 1
  7. Line 2 blah blah blah
  8. 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");
});

  1. while (input.contains("\n\n")){
  2. input = input.replace("\n\n","\n");
  3. }
  4. // 修剪两侧的边距
  5. input = input.trim();
  6. System.out.println(input);
  7. }

}

结果

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

英文:

Project structure

input.txt

  1. This is test;
  2. This is new test;
  3. This is new test2;
  4. This is another string test;
  5. this is more space string test;

Main.clas

  1. public class Main {
  2. private static String input = "";
  3. public static void main(String[] args) throws IOException {
  4. //Reading the file, taking into account all spaces and margins.
  5. Files.lines(Paths.get("src/input.txt"), StandardCharsets.UTF_8).forEach(e->{
  6. input = input.concat(e);
  7. input = input.concat("\n");
  8. });
  9. while (input.contains("\n\n")){
  10. input = input.replace("\n\n","\n");
  11. }
  12. //trim the margins on both sides
  13. input = input.trim();
  14. System.out.println(input);
  15. }
  16. }

Result

  1. This is test;
  2. This is new test;
  3. This is new test2;
  4. This is another string test;
  5. this is more space string test;

答案3

得分: 0

尝试这个。

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

输出

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

Try this.

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

output

  1. This is test;
  2. This is new test;
  3. This is new test2;
  4. This is another string test;
  5. 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:

确定