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.