Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/392.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Java中以给定的字符串格式添加分隔符?_Java_Regex - Fatal编程技术网

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

如何在Java中以给定的字符串格式添加分隔符?,java,regex,Java,Regex,我有以下字符串 "12:00:00, 2:30:003:45:00,23:45:00"; 我必须更新字符串以使用以下格式: "12:00:00, 2:30:00 |3:45:00,23:45:00 "; 我可以拆分每个字符串,但我不知道如何生成所需的格式。以下是我迄今为止编写的代码: final String s=“12:00:00,2:30:003:45:00,23:45:00”; 最终模式p=Pattern.compile(\\s*(\\d+:\\d\\d:\\d\\d)”; 最终匹配器

我有以下字符串

"12:00:00, 2:30:003:45:00,23:45:00";
我必须更新字符串以使用以下格式:

"12:00:00, 2:30:00 |3:45:00,23:45:00 ";
我可以拆分每个字符串,但我不知道如何生成所需的格式。以下是我迄今为止编写的代码:

final String s=“12:00:00,2:30:003:45:00,23:45:00”;
最终模式p=Pattern.compile(\\s*(\\d+:\\d\\d:\\d\\d)”;
最终匹配器m=p匹配器;
最终列表标记=新的ArrayList();
while(m.find()){
添加(m.group(1));
}
for(字符串tok:令牌){
系统输出打印F(“[%s]%n”,tok);
}
这个怎么样:

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);
final String=“12:00:00,2:30:003:45:00,23:45:00”;
最终模式=模式。编译(\\s*(\\d+:\\d\\d:\\d\\d)”;
final Matcher Matcher=pattern.Matcher(字符串);
最终列表标记=新的ArrayList();
while(matcher.find()){
添加(匹配器组(1));
}
System.out.println(“tokens=“+tokens”);
StringBuilder formattedString=新建StringBuilder();
append(tokens.get(0));
for(inti=1;i

编辑:我已经更新了它,在根据我读到的注释构造格式化字符串时使用for循环。

如果您想在逗号分隔的两个日期之后添加
|
,您的代码可以如下所示

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|");
甚至

String result = s.replaceAll("?:\\d+:\\d\\d:\\d\\d),\\s*(?:\\d+:\\d\\d:\\d\\d)","$0|");
$0
指保存整个匹配项的组0


<> >代码>结果<代码> >代码> 12:00 00,2时30分> 3:45:00,23:45时,< /代码>

< p>你可以考虑这个代码>使用LokBoosis:/P>
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|
final String s=“12:00:00,2:30:003:45:00,23:45:00”;

System.out.printf(“%s%n”,s.replaceAll(“(?但是
6:34:00,5:12:00
中没有输入?我错误地这样做了,您可以使用input@anubhava你能帮忙吗???你的要求不清楚。你到底想在文本中添加什么?首先你要添加
124;
(其中
124;
是空格)但是你要添加的是
124;
。那么是哪一个呢?还要“让字符串像。。。"并不能解释一切。你需要更精确。你想只在某些特定日期之后添加分隔符吗?还是想在用逗号分隔的两个日期之后添加分隔符?或者可能有其他方法来决定应在何处添加分隔符?我希望输出为一个字符串,就像参数的数量在不同的日期可以不同一样每个案例…@lucifer好的,我已经更新了代码。我认为它现在可以正常工作了。