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

时间:2019-05-16 06:02:38

标签: java string split substring

我有一个这样的字符串:"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];
}

4 个答案:

答案 0 :(得分:2)

这是使用流的Java 8解决方案。逻辑是先用逗号分割输入字符串,生成一个术语数组。然后,对于每个术语,我们再次在破折号处分开,保留第一个条目。如果术语没有破折号,则整个字符串将被保留。最后,我们将其连接回一个输出字符串。

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

答案 1 :(得分:1)

按照解决问题的方式,实际上应该先用逗号分隔。然后,您应该遍历对split的调用结果,并使用连字符将输出数组中的每个字符串(如果存在的话)分开。如果没有连字符,则可以将0添加到字符串2,将字符串本身添加到字符串1。如果存在连字符,请将左侧添加到字符串1,将右侧添加到字符串2 。这是您可以执行此操作的一种方法,

if(rawIndication.contains(",")){
    String s1 = "{";
    String s2 = "{";
    String[] parts = rawIndication.split(",");
    for(int i = 0; i < parts.length; i++) {
        if(parts[i].contains("-") {
            String[] moreParts = parts[i].split(",");
            s1 = s1 + moreParts[0] + ",";
            s2 = s2 + moreParts[1] + ",";
        }
        else{
            s1 = s1 + parts[i] + ",";
            s2 = "0,";
        }
    }
    s1 = s1.substring(0, s1.length() - 1); //remove last extra comma
    s2 = s2.substring(0, s2.length() - 1); //remove last extra comma
    s1 = s1 + "}";
    s2 = s2 + "}";
}

答案 2 :(得分:1)

    List<String> list1 = new ArrayList<>();
    List<String> list2 = new ArrayList<>();

    // First split the source String by comma to separate main parts
    String[] mainParts = sourceStr.split(",");

    for (String mainPart: mainParts) {
        // Check if each part contains '-' character
        if (mainPart.contains("-")) {
            // If contains '-', split and add the 2 parts to 2 arrays
            String[] subParts = mainPart.split("-");
            list1.add(subParts[0]);
            list2.add(subParts[1]);

        } else {
            // If does not contain '-', add complete part to 1st array and add placeholder to 2nd array
            list1.add(mainPart);
            list2.add("0");
        }
    }

    // Build the final Strings by joining String parts by commas and enclosing between parentheses
    String str1 = "{" + String.join(",", list1) + "}";
    String str2 = "{" + String.join(",", list2) + "}";

    System.out.println(str1);
    System.out.println(str2);

答案 3 :(得分:1)

我认为这可以解决您的问题。

private static void splitStrings() {
   List<String> list = Arrays.asList("aa-bb", "ccdd", "eeff", "gg-gg", "cc-gg");
    List firstPartList = new ArrayList<>();
    List secondPartList = new ArrayList<>();
    for (String undividedString : list){
        if(undividedString.contains("-")){
            String[] dividedParts = undividedString.split("-");
            String firstPart = dividedParts[0];
            String secondPart = dividedParts[1];
            firstPartList.add(firstPart);
            secondPartList.add(secondPart);
        } else{
            firstPartList.add(undividedString);
            secondPartList.add("0");
        }
    }

    System.out.println(firstPartList);
    System.out.println(secondPartList);
}

输出为-

[aa, ccdd, eeff, gg, cc]
[bb, 0, 0, gg, gg]