-1

I am trying to create a method that substrings a string from the occurrence from one word(labelA) to another(labelB) and vice versa.

If the string is The Number2 quick Number1 onyx goblin jumps Number1 over the lazy dwarf Number2

I am trying to get the strings which are between labels to be assigned to Sub variable.

So it outputs vector will have value similar to this in each run of while loop

temp.addElement("Number2");
temp.add("The");

Then

temp.addElement("Number1");
temp.add("quick ");

Then

temp.addElement("Number1");
temp.add("onyx goblin jumps  ");

Then

temp.addElement("Number2");
temp.add("over the lazy dwarf  ");

This is what I have tried

 private Vector cutter(String str){
    Vector thisPlan - new Vector();
    Vector temp = new Vector();
    try{
       int i =0;
       int end = 0;
       String labelA = "Number1:";
       String labelB = "Number2:";
       String Sub = "";
       int pos = 0;
       while(i<r.length()){
         try{
             pos = str.indexOf(labelA, pos);
             Sub = str.substring(pos,str.length());
             pos++;
         }catch(Exception e){
             pos = str.indexOf)(labelB, pos);
             Sub = str.substring(pos,str.length());
              pos++;
         }
         if(!labelA.equals(labelB) || (labelA.equals(labelB) && !StringUtil.scrub(Sub).equals(""))){
          temp.addElement(labelA);
          temp.add(Sub);
         }
         if(!labelA.equals(labelB) || (labelA.equals(labelB) && !StringUtil.scrub(Sub).equals(""))){
           temp.addElement(labelB);
           temp.add(Sub);
          }
          temp = new Vector();
      } catch (Exception e){
       }
      return thisPlan;
    }
}

Please help, and at least give suggestion how can I accomplish this.

Somnath Musib
  • 3,548
  • 3
  • 34
  • 47
J.Doe
  • 27
  • 6

1 Answers1

1

String.split(String) will I believe do the trick:

"The Number2 quick Number1 onyx goblin jumps Number1 over the lazy dwarf Number2 ")
    .split("Number2|Number1")

equals

["The ", " quick ", " onyx goblin jumps ", " over the lazy dwarf ", " "]

which looks a lot like what I believe you are seeking.

To also keep the separating "Number1" and "Number2" delimiting strings you can use zero-width positive lookahead and lookbehind expressions as used in https://stackoverflow.com/a/19951885/1840078 and How to split a string, but also keep the delimiters?:

"The Number2 quick Number1 onyx goblin jumps Number1 over the lazy dwarf Number2 "
    .split("(?=Number[12])|(?<=Number[12])")

equals

["The ", "Number2", " quick ", "Number1", " onyx goblin jumps ",
    "Number1", " over the lazy dwarf ", "Number2", " "]
Mark A. Fitzgerald
  • 1,249
  • 1
  • 9
  • 20
  • 1
    Yeah can you please elaborate – J.Doe Oct 20 '17 at 03:14
  • 1
    Thanks alot got it – J.Doe Oct 20 '17 at 03:17
  • You're welcome! I was just about to add some `String[]` to `Vector` conversion code in a `private Vector cutter(String str)` method to my answer. Glad you were able to get there yourself. :) – Mark A. Fitzgerald Oct 20 '17 at 03:19
  • 1
    How can I print the the Label which occured before the substring – J.Doe Oct 20 '17 at 03:35
  • 1
    As that will be needed to put in vector too like Number1 or Number2 – J.Doe Oct 20 '17 at 03:36
  • Storing both labels and in-between texts in a single `Vector` would be a bit different problem with moderately different solution code. If you could you post that as a separate question I'd be happy to answer there. – Mark A. Fitzgerald Oct 20 '17 at 03:39
  • In brief I'd probably use `Pattern` and `Matcher` with a more complex regex to meet the 'also store labels' need. More specifically I'd leverage either `Matcher.find()` or `Matcher.group(int)` depending which pattern best suits your needs. I could say more in an Answer to that Question. :) – Mark A. Fitzgerald Oct 20 '17 at 03:52