将Java中的字符串解析为一行。

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

Parsing string in java to get it all on one line

问题

String s = "ABC123,DEF,TEST,JJJ,123456 0 789123 2 555555 3";

到目前为止,我已经做了以下工作:

String s = s.replaceAll("\R+", "");

这给了我不是我想要的:

String s = "ABC123,DEF,TEST,JJJ,123456 0789123 2555555 3";

这在某种程度上有效,但所有的数字都粘在一起,这并不有助于我,所以我现在遇到了难题。

英文:

I have String that looks like this:

  1. String s = "ABC123,DEF,
  2. TEST,JJJ,
  3. 123456 0
  4. 789123 2
  5. 555555 3"

and I want to parse it so it looks like

  1. String s = "ABC123,DEF,TEST,JJJ,123456 0 789123 2 555555 3

So far what I have done is:

  1. String s = s.replaceAll("\\R+",""));

this gave me not what I wanted:

  1. String s = "ABC123,DEF,TEST,JJJ,123456 0789123 2555555 3

This works to an extent but all the numbers are stuck together which doesn't help so I'm at roadblock right now.

答案1

得分: 1

你可以将以,前导的换行符替换为空字符串,然后将其链接以仅替换换行符为空格。

  1. s = s.replaceAll(",\\R+", "").replaceAll("\\R+", " ");
  2. System.out.println(s);

输出:

  1. ABC123 DEFTEST JJJ123456 0 789123 2 555555 3
英文:

You can replace line breaks preceded by , with an empty string and then chain it to replace just the line breaks with whitespace.

  1. s = s.replaceAll(",\\R+","").replaceAll("\\R+"," ");
  2. System.out.println(s);

Output:

  1. ABC123,DEFTEST,JJJ123456 0 789123 2 555555 3

答案2

得分: 1

你可以将所有的空白序列减少为一个空格,然后移除逗号后面的空格:

  1. s.replaceAll("\\R+", " ").replaceAll(", ", ",")
英文:

You can reduce all whitespaces sequence to one space, then remove the space after commas

  1. s.replaceAll("\\R+"," ").replaceAll(", ", ",")

答案3

得分: 0

以下是翻译好的部分:

  1. *`and I want to parse it so it looks like`*<br>
  2. `String s = "ABC123,DEF,TEST,JJJ,123456 0 789123 2 555555 3`
  3. 当前带有换行符的字符串

String s = """
ABC123,DEF,
TEST,JJJ,
123456 0
789123 2
555555 3
""";

  1. - 首先用空格替换换行符
  2. - 然后将`", "`替换为单个逗号。这里不需要正则表达式。
  3. ```java
  4. String result = s.replaceAll("\\R+"," ").replace(", ",",");
  5. System.out.println(result);

输出

  1. ABC123,DEF,TEST,JJJ,123456 0 789123 2 555555 3
英文:

and I want to parse it so it looks like<br>
String s = &quot;ABC123,DEF,TEST,JJJ,123456 0 789123 2 555555 3

Current string with line terminators.

  1. String s = &quot;&quot;&quot;
  2. ABC123,DEF,
  3. TEST,JJJ,
  4. 123456 0
  5. 789123 2
  6. 555555 3
  7. &quot;&quot;&quot;;
  • first replace the line terminators with a space
  • then replace the &quot;, &quot; with a single comma. No regex needed here.
  1. String result = s.replaceAll(&quot;\\R+&quot;,&quot; &quot;).replace(&quot;, &quot;,&quot;,&quot;);
  2. System.out.println(result);

prints

  1. ABC123,DEF,TEST,JJJ,123456 0 789123 2 555555 3
  2. </details>
  3. # 答案4
  4. **得分**: 0
  5. ```none
  6. 由于条件似乎是检查行是否以逗号结尾,只需使用for循环。
  7. ```java
  8. String[] strings = s.split(&quot;\\R+&quot;);
  9. s = &quot;&quot;;
  10. for (String string : strings) {
  11. s += string;
  12. if (!string.endsWith(&quot;,&quot;)) s += &quot; &quot;;
  13. }

输出

  1. ABC123,DEF,TEST,JJJ,123456 0 789123 2 555555 3

值得注意的是,某些输出将具有尾随的空白字符,您可以使用String#trim方法来处理此问题,或者在for循环内添加额外的检查。

  1. <details>
  2. <summary>英文:</summary>
  3. Since the condition appears to be whether or not the line ends with a _comma_, just use a _for-loop_.
  4. ```java
  5. String[] strings = s.split(&quot;\\R+&quot;);
  6. s = &quot;&quot;;
  7. for (String string : strings) {
  8. s += string;
  9. if (!string.endsWith(&quot;,&quot;)) s += &quot; &quot;;
  10. }

Output

  1. ABC123,DEF,TEST,JJJ,123456 0 789123 2 555555 3

It's worth noting some outputs will have a trailing white-space character, you can use the String#trim method for this, or just place an additional check within the for-loop.

huangapple
  • 本文由 发表于 2023年8月4日 00:41:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/76830078.html
匿名

发表评论

匿名网友

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

确定