0

I want to split a string based message based on the definition.

    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

Ideally, if there is an existing mechanism in java or spring which splits the message and instantiates the object that would be ideal.

M. Deinum
  • 115,695
  • 22
  • 220
  • 224
Vikrant
  • 63
  • 1
  • 1
  • 8
  • 1
    Possible duplicate of [How to split a string in Java](https://stackoverflow.com/questions/3481828/how-to-split-a-string-in-java) – UmarZaii Sep 12 '17 at 19:00

4 Answers4

2

This is exactly what substring does...

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;
}

One thing you need to do is translate rest to -1, for example:

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

Then you can use split(String, String) to achieve your ways, for example:

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"]
holi-java
  • 29,655
  • 7
  • 72
  • 83
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

Use StringBuilder, everytime you get substring followed by deletion of substring. Make use of substring(int start, int end) method and delete(int start, int end)

Rahul Raj
  • 3,197
  • 5
  • 35
  • 55
  • The entire `StringBuilder` manipulation is obsolete, as you can perform `substring` directly on the original string just adapting the offsets instead of deleting content, `String s = "AAABBB CCDDD FFGGGHHHI KKK"; System.out.println(s.substring(0, 2)); System.out.println(s.substring(2, 8));`. You can even print subsequences in the first place, `System.out.append(s, 0, 2).append('\n').append(s, 2, 8).println();`… – Holger Sep 13 '17 at 09:37
  • yeah, we can use `String`s `substring()` method to print, but I thought OP wanted to perform `substring()` operation on an updated `String`. Since `StringBuilder` is mutable, I used that instead of creating additional `String` variables. – Rahul Raj Sep 13 '17 at 12:28
0

First you made an error in your counting, and this is some code to do your stuff :

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);
}

And the output is :

AA         // 2
ABBB C     // 6
CDD        // 3
D          // 1
    FF     // 6
GGGHHHI    // 7
     KKK   // rest
azro
  • 53,056
  • 7
  • 34
  • 70
0

That is my version

    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;
}

Output:

2   : AA
6   : ABBB C
3   : CDD
1   : D
6   :     FF
7   : GGGHHHI
rest:      KKK
stefan bachert
  • 9,413
  • 4
  • 33
  • 40