英文:
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:
String s = "ABC123,DEF,
TEST,JJJ,
123456 0
789123 2
555555 3"
and I want to parse it so it looks like
String s = "ABC123,DEF,TEST,JJJ,123456 0 789123 2 555555 3
So far what I have done is:
String s = s.replaceAll("\\R+",""));
this gave me not what I wanted:
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
你可以将以,
前导的换行符替换为空字符串,然后将其链接以仅替换换行符为空格。
s = s.replaceAll(",\\R+", "").replaceAll("\\R+", " ");
System.out.println(s);
输出:
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.
s = s.replaceAll(",\\R+","").replaceAll("\\R+"," ");
System.out.println(s);
Output:
ABC123,DEFTEST,JJJ123456 0 789123 2 555555 3
答案2
得分: 1
你可以将所有的空白序列减少为一个空格,然后移除逗号后面的空格:
s.replaceAll("\\R+", " ").replaceAll(", ", ",")
英文:
You can reduce all whitespaces sequence to one space, then remove the space after commas
s.replaceAll("\\R+"," ").replaceAll(", ", ",")
答案3
得分: 0
以下是翻译好的部分:
*`and I want to parse it so it looks like`*<br>
`String s = "ABC123,DEF,TEST,JJJ,123456 0 789123 2 555555 3`
当前带有换行符的字符串。
String s = """
ABC123,DEF,
TEST,JJJ,
123456 0
789123 2
555555 3
""";
- 首先用空格替换换行符
- 然后将`", "`替换为单个逗号。这里不需要正则表达式。
```java
String result = s.replaceAll("\\R+"," ").replace(", ",",");
System.out.println(result);
输出
ABC123,DEF,TEST,JJJ,123456 0 789123 2 555555 3
英文:
and I want to parse it so it looks like
<br>
String s = "ABC123,DEF,TEST,JJJ,123456 0 789123 2 555555 3
Current string with line terminators.
String s = """
ABC123,DEF,
TEST,JJJ,
123456 0
789123 2
555555 3
""";
- first replace the line terminators with a space
- then replace the
", "
with a single comma. No regex needed here.
String result = s.replaceAll("\\R+"," ").replace(", ",",");
System.out.println(result);
prints
ABC123,DEF,TEST,JJJ,123456 0 789123 2 555555 3
</details>
# 答案4
**得分**: 0
```none
由于条件似乎是检查行是否以逗号结尾,只需使用for循环。
```java
String[] strings = s.split("\\R+");
s = "";
for (String string : strings) {
s += string;
if (!string.endsWith(",")) s += " ";
}
输出
ABC123,DEF,TEST,JJJ,123456 0 789123 2 555555 3
值得注意的是,某些输出将具有尾随的空白字符,您可以使用String#trim方法来处理此问题,或者在for循环内添加额外的检查。
<details>
<summary>英文:</summary>
Since the condition appears to be whether or not the line ends with a _comma_, just use a _for-loop_.
```java
String[] strings = s.split("\\R+");
s = "";
for (String string : strings) {
s += string;
if (!string.endsWith(",")) s += " ";
}
Output
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论