代码之家  ›  专栏  ›  技术社区  ›  ant

Java中的复杂字符串拆分

  •  2
  • ant  · 技术社区  · 14 年前

    考虑以下字符串:

    5|12345|value1|value2|value3|value4+5|777|value1|value2|value3|value4?5|777|value1|value2|value3|value4+
    

    下面是我想要拆分字符串的方法,用+拆分它,这样我得到了这个结果:

    myArray[0] = "5|12345|value1|value2|value3|value4";
    
    myArray[1] = "5|777|value1|value2|value3|value4?5|777|value1|value2|value3|value4";
    

    如果字符串has不包含char "?" 把它拆开 "|" 如果字符串确实包含 “?” 拆分它 对于每一部分 把它拆开 “是” 继续第二部分。

    以下是第二部分:

    myObject.setAttribute1(newString[0]);
    ...
    myObject.setAttribute4(newString[3]);
    

    以下是迄今为止我所得到的:

    private static String input = "5|12345|value1|value2|value3|value4+5|777|value1|value2|value3|value4?5|777|value1|value2|value3|value4+";
    
    public void mapObject(String input){
    
    String[] myArray = null;
    
        if (input.contains("+")) {
            myArray = input.split("+");
        } else {
            myArray = new String[1];
            myArray[0] = input;
        }
    
        for (int i = 0; i < myArray.length; i++) {
    
            String[] secondaryArray = null;
            String[] myObjectAttribute = null;
    
            if (myArray[i].contains("?")) {
                secondaryArray = temporaryString.myArray[i].split("?");
    
                for (String string : secondaryArray) {
                    myObjectAttribute = string.split("\\|");
                }
            } else {
                myObjectAttribute = myArray[i].toString().split("\\|");
            }
    
            myObject.setAttribute1(myObjectAttribute[0]);
            ...
            myObject.setAttribute4(myObjectAttribute[3]);
                        System.out.println(myObject.toString());
    }
    

    问题:

    当我用我的数组[0]拆分我的数组时,一切都设置得很好。

    然后是myarray[1],它分成两部分,然后第二部分覆盖第一部分的值(我怎么知道?)我已经重写了 toString() 方法myObject,当我完成打印设置值时,我知道它会覆盖它,有人知道如何修复它吗?

    2 回复  |  直到 14 年前
        1
  •  2
  •   mdma    14 年前

    代码似乎无法在第二种情况下处理字符串的可能扩展。为了使代码更清晰,我将使用列表而不是数组。

    private static String input = "5|12345|value1|value2|value3|value4+5|777|value1|value2|value3|value4?5|777|value1|value2|value3|value4+";
    
    private void split(List<String> input, List<String> output, String split) {
        for (String s: input) {
              if (s.contains(split))
              {
                 output.addAll(Arrays.asList(s.split(Pattern.quote(split)));
              }
              else
                  output.add(s);
        }
    }
    

    公共void映射对象(字符串输入){

    List<String> inputSrings = new ArrayList<String>();
    List<String> splitPlus = new ArrayList<String>();
    
    inputStrings.add(input);
    
    split(inputStrings, splitPlus);
    
    List<String> splitQuest = new ArrayList<String>();
    split(splitPlus, splitQuest, "?");
    
    for (String s: splitQuest) {   
       // you can now set the attributes from the values in the list
       // splitPipe
    
       String[] attributes = s.split("\\|");
       myObject.setAttribute1(attributes[0]);
       ....
       myObject.setAttribute4(attributes[3]);
       System.out.println(myObject);
    }
    

    }

        2
  •  3
  •   matt b    14 年前

    我不太确定这里的目的是什么,但在这段代码中

    secondaryArray = temporaryString.split("?");
    
    for (String string : secondaryArray) {
        myObjectAttribute = string.split("\\|");
    }
    

    如果 secondaryArray 在分割操作之后有两个元素,您将遍历每一半并重新分配 myObjectAttribute 到string.split(“”)每次的输出。在第一个元素中有什么并不重要 二次阵列 ,就像运行此代码之后一样 对象属性 将包含的结果 split("\\|") 在数组的最后一个元素上。

    而且,打电话也没有意义 .toString() 在字符串对象上执行 temporaryString = myArray[i].toString() .