按条件拆分和配对子字符串

我有一个这样的字符串:"aa-bb,ccdd,eeff,gg-gg,cc-gg". 我需要按'-'符号拆分字符串并从中创建两个字符串,但如果原始字符串的逗号分隔部分不包含'-',则需要使用一些占位符字符而不是子字符串。在上面的示例输出的情况下应该是:


字符串 1:


"{aa,ccdd,eeff,gg,cc}"

字符串 2:


"{bb,0,0,gg,gg}"

我不能使用该lastIndexOf()方法,因为输入在一个字符串中。我不确定零件有多少。


if(rawIndication.contains("-")){

    String[] parts = rawIndication.split("-");

    String part1 = parts[0];

    String part2 = parts[1];

}


www说
浏览 153回答 3
3回答

至尊宝的传说

这是一个使用流的 Java 8 解决方案。逻辑是首先用逗号分割输入字符串,生成一个术语数组。然后,对于每个术语,我们在 dash 上再次拆分,保留第一个条目。对于没有破折号的术语,将只保留整个字符串。最后,我们连接回输出字符串。String input = "aa-bb,ccdd,eeff,gg-gg,cc-gg";int pos = 1;String output = String.join(",", Arrays.stream(parts)     .map(e -> e.split("-").length >= (pos+1) ? e.split("-")[pos] : "0")    .toArray(String[]::new));System.out.println(output);这输出:bb,0,0,gg,gg

泛舟湖上清波郎朗

&nbsp; &nbsp; List<String> list1 = new ArrayList<>();&nbsp; &nbsp; List<String> list2 = new ArrayList<>();&nbsp; &nbsp; // First split the source String by comma to separate main parts&nbsp; &nbsp; String[] mainParts = sourceStr.split(",");&nbsp; &nbsp; for (String mainPart: mainParts) {&nbsp; &nbsp; &nbsp; &nbsp; // Check if each part contains '-' character&nbsp; &nbsp; &nbsp; &nbsp; if (mainPart.contains("-")) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // If contains '-', split and add the 2 parts to 2 arrays&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String[] subParts = mainPart.split("-");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; list1.add(subParts[0]);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; list2.add(subParts[1]);&nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // If does not contain '-', add complete part to 1st array and add placeholder to 2nd array&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; list1.add(mainPart);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; list2.add("0");&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; // Build the final Strings by joining String parts by commas and enclosing between parentheses&nbsp; &nbsp; String str1 = "{" + String.join(",", list1) + "}";&nbsp; &nbsp; String str2 = "{" + String.join(",", list2) + "}";&nbsp; &nbsp; System.out.println(str1);&nbsp; &nbsp; System.out.println(str2);

四季花海

我认为这可以解决您的问题。private static void splitStrings() {&nbsp; &nbsp;List<String> list = Arrays.asList("aa-bb", "ccdd", "eeff", "gg-gg", "cc-gg");&nbsp; &nbsp; List firstPartList = new ArrayList<>();&nbsp; &nbsp; List secondPartList = new ArrayList<>();&nbsp; &nbsp; for (String undividedString : list){&nbsp; &nbsp; &nbsp; &nbsp; if(undividedString.contains("-")){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String[] dividedParts = undividedString.split("-");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String firstPart = dividedParts[0];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String secondPart = dividedParts[1];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; firstPartList.add(firstPart);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; secondPartList.add(secondPart);&nbsp; &nbsp; &nbsp; &nbsp; } else{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; firstPartList.add(undividedString);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; secondPartList.add("0");&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; System.out.println(firstPartList);&nbsp; &nbsp; System.out.println(secondPartList);}输出是 -[aa, ccdd, eeff, gg, cc][bb, 0, 0, gg, gg]
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java