有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何基于逗号拆分字符串

我现在有一个字符串,它是逗号和空格的组合。为了按顺序获取特定元素,我发布了这个字符串

12:00am, 2:30am3:30am, 5:00am7:45pm,9:00pm

这是我正在尝试使用拆分字符串的字符串

List<String> interviewTimingToFrom1 = Arrays.asList(interviewTime1.split(","));

但这不起作用,我只想把12:00am,3:30am,7:45pm分为一个列表,把2:30am,5:00am,9:00am分为一个列表如何做,请有人帮忙


共 (1) 个答案

  1. # 1 楼答案

    您可以使用此正则表达式:

    List<String> interviewTimingToFrom1 = Arrays.asList(
                        interviewTime1.split("\\s*,\\s*|(?<=[ap]m)(?=\\d)"));
    

    RegEx Demo

    正则表达式拆分

    拆分使用:

    \s*,\s*          # comma surrounded by optional spaces on either sides
    |                # regex alternation
    (?<=[ap]m)(?=\d) # when preceding text is am or pm and following character is a digit