拆分字符串并实例化对象

时间:2017-09-12 18:45:59

标签: java spring java-8

我想根据定义拆分基于字符串的消息。

    String: AAABBB CCDDD    FFGGGHHHI     KKK
    Definition: 2,6,3,1,6,7,rest
    So,
    2   AA
    6   ABBB C
    3   CDD
    1   D
    6        F
    7   FGGGHHH
    rest    I     KKK

理想情况下,如果java或spring中存在一个现有机制,它会拆分消息并实例化理想的对象。

4 个答案:

答案 0 :(得分:2)

这正是substring所做的......

static String[] split(String source, int ... sizes){
  int n = source.length(), start = 0;
  String [] partials = new String[sizes.length];

  for(int i = 0; i < sizes.length; i++){
    int end = sizes[i] < 0 ? n : start + sizes[i];
    partials[i] = source.substring(start, start = end);
  }

  return partials;
}

您需要做的一件事是将rest翻译为-1,例如:

static String[] split(String source, String sizes){
  return split(source, Arrays.stream(sizes.split(","))
                    .mapToInt(it -> it.equals("rest") ? -1 : Integer.parseInt(it))
                    .toArray());
}

然后您可以使用split(String, String)来实现自己的方式,例如:

String string = "AAABBB CCDDD    FFGGGHHHI     KKK"; 
String definition = "2,6,3,1,6,7,rest";

String[] results = split(string, definition);
//        ^--- ["AA", "ABBB C", "CDD", "D", "    FF", "GGGHHHI", "     KKK"]

答案 1 :(得分:0)

StringBuilder temp=new StringBuilder("AAABBB CCDDD    FFGGGHHHI     KKK");
System.out.println(temp.substring(0,2));
temp.delete(0,2);
System.out.println(temp.substring(0,6));
temp.delete(0,6);
//it goes like this

使用StringBuilder,每次获得子字符串,然后删除子字符串。 使用substring(int start, int end)方法和delete(int start, int end)

答案 2 :(得分:0)

首先你在计算时出错了,这是一些代码来做你的事情:

public static void main(String[] args) {
    String str = "AAABBB CCDDD    FFGGGHHHI     KKK";
    int[] split = new int[]{2, 6, 3, 1, 6, 7};
    List<String> res = new ArrayList<>();
    StringBuilder temp = new StringBuilder(str);

    for (int i = 0; i < split.length ; i++) {
        res.add(temp.substring(0, split[i]));
        temp.delete(0, split[i]);
    }
    res.add(temp.toString());

    res.forEach(System.out::println);
}

输出是:

AA         // 2
ABBB C     // 6
CDD        // 3
D          // 1
    FF     // 6
GGGHHHI    // 7
     KKK   // rest

答案 3 :(得分:0)

那是我的版本

    String str = "AAABBB CCDDD    FFGGGHHHI     KKK";
    List<Integer> def = Arrays.asList(2,6,3,1,6,7);

    List<String> res = split (str, def);
    int n = res.size();
    for (int i = 0; i < n; i ++) {
        String r = res.get(i);
        if (i + 1 == n) {
            System.out.println("rest: " + r);
        } else {
            System.out.println(r.length() + "   : " + r);
        }
    }

public List<String> split (String pStr, List<Integer> pDef) {
    List<String> ret = new ArrayList<String> ();
    int startStr = 0;
    int startDef = 0;
    while (startStr < pStr.length() && startDef < pDef.size()) {
        int end = startStr + pDef.get(startDef);
        ret.add(pStr.substring(startStr, end));
        startStr = end;
        startDef ++;
    }
    if (startStr < pStr.length()) {
        ret.add(pStr.substring(startStr));
    }
    return ret;
}

输出:

2   : AA
6   : ABBB C
3   : CDD
1   : D
6   :     FF
7   : GGGHHHI
rest:      KKK