如何在Java中以给定的String格式添加分隔符?

时间:2022-09-22 23:52:14

I have the following String

我有以下字符串

"12:00:00, 2:30:003:45:00,23:45:00";

I have to update the string to use the following format:

我必须更新字符串以使用以下格式:

"12:00:00, 2:30:00 |3:45:00,23:45:00 ";

I am able to split each string, but I do not know how to generate the required format. Here is the code I've written so far:

我能够分割每个字符串,但我不知道如何生成所需的格式。这是我到目前为止编写的代码:

final String s = "12:00:00, 2:30:003:45:00,23:45:00";

final Pattern p = Pattern.compile("\\s*(\\d+:\\d\\d:\\d\\d)");
final Matcher m = p.matcher(s);
final List<String> tokens = new ArrayList<String>();
while (m.find()) {
    tokens.add(m.group(1));
}
for (String tok : tokens) {
    System.out.printf("[%s]%n", tok);
}

3 个解决方案

#1


1  

How about this:

这个怎么样:

final String string = "12:00:00, 2:30:003:45:00,23:45:00";

final Pattern pattern = Pattern.compile("\\s*(\\d+:\\d\\d:\\d\\d)");
final Matcher matcher = pattern.matcher(string);
final List<String> tokens = new ArrayList<String>();
while (matcher.find()) {
    tokens.add(matcher.group(1));
}
System.out.println("tokens = " + tokens);

StringBuilder formattedString = new StringBuilder();
formattedString.append(tokens.get(0));
for (int i = 1; i < tokens.size(); i++) {
    if (i % 2 == 0) {
        formattedString.append(" | ");
    } else {
        formattedString.append(", ");
    }
    formattedString.append(tokens.get(i));
}
System.out.println(formattedString);

Edit: I've updated it to use a for loop when constructing the formatted string based on the comments I've read.

编辑:我根据我读过的评论构建格式化字符串时更新了它以使用for循环。

#2


1  

If you want to add | after two dates separated by comma your code can look like

如果你想添加|在用逗号分隔的两个日期之后,您的代码可能看起来像

final String s = "12:00:00, 2:30:003:45:00,23:45:00";

final Pattern p = Pattern.compile("(\\d+:\\d\\d:\\d\\d)\\s*,\\s*(\\d+:\\d\\d:\\d\\d)");
final Matcher m = p.matcher(s);
String result = m.replaceAll("$0|");

Or even

String result = s.replaceAll("?:\\d+:\\d\\d:\\d\\d),\\s*(?:\\d+:\\d\\d:\\d\\d)","$0|");

$0 refers to group 0 which holds entire match.

$ 0指的是保持整场比赛的0组。

result is 12:00:00, 2:30:00|3:45:00,23:45:00|

结果是12:00:00,2:30:00 | 3:45:00,23:45:00 |

#3


1  

You may consider this replaceAll method using lookarounds:

你可以使用lookarounds来考虑这个replaceAll方法:

final String s = "12:00:00, 2:30:003:45:00,23:45:00";
System.out.printf("%s%n", s.replaceAll("(?<=:\\d\\d)(?=(?::\\d{1,2}|$))", "|"));

// 12:00|:00, 2:30|:003:45|:00,23:45|:00|

#1


1  

How about this:

这个怎么样:

final String string = "12:00:00, 2:30:003:45:00,23:45:00";

final Pattern pattern = Pattern.compile("\\s*(\\d+:\\d\\d:\\d\\d)");
final Matcher matcher = pattern.matcher(string);
final List<String> tokens = new ArrayList<String>();
while (matcher.find()) {
    tokens.add(matcher.group(1));
}
System.out.println("tokens = " + tokens);

StringBuilder formattedString = new StringBuilder();
formattedString.append(tokens.get(0));
for (int i = 1; i < tokens.size(); i++) {
    if (i % 2 == 0) {
        formattedString.append(" | ");
    } else {
        formattedString.append(", ");
    }
    formattedString.append(tokens.get(i));
}
System.out.println(formattedString);

Edit: I've updated it to use a for loop when constructing the formatted string based on the comments I've read.

编辑:我根据我读过的评论构建格式化字符串时更新了它以使用for循环。

#2


1  

If you want to add | after two dates separated by comma your code can look like

如果你想添加|在用逗号分隔的两个日期之后,您的代码可能看起来像

final String s = "12:00:00, 2:30:003:45:00,23:45:00";

final Pattern p = Pattern.compile("(\\d+:\\d\\d:\\d\\d)\\s*,\\s*(\\d+:\\d\\d:\\d\\d)");
final Matcher m = p.matcher(s);
String result = m.replaceAll("$0|");

Or even

String result = s.replaceAll("?:\\d+:\\d\\d:\\d\\d),\\s*(?:\\d+:\\d\\d:\\d\\d)","$0|");

$0 refers to group 0 which holds entire match.

$ 0指的是保持整场比赛的0组。

result is 12:00:00, 2:30:00|3:45:00,23:45:00|

结果是12:00:00,2:30:00 | 3:45:00,23:45:00 |

#3


1  

You may consider this replaceAll method using lookarounds:

你可以使用lookarounds来考虑这个replaceAll方法:

final String s = "12:00:00, 2:30:003:45:00,23:45:00";
System.out.printf("%s%n", s.replaceAll("(?<=:\\d\\d)(?=(?::\\d{1,2}|$))", "|"));

// 12:00|:00, 2:30|:003:45|:00,23:45|:00|