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.

1

4 Answers 4

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"]
Sign up to request clarification or add additional context in comments.

Comments

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)

2 Comments

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();
yeah, we can use Strings 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.
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

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.