I need to write a function which will do the following functionalities
I would get a string and a array as input parameters
The string would be like one of the following for example
catalog
level1Cats
level2Cats
The String array would be containing the data for the strings mentioned above for example
catalog:("12605")
level1Cats:("12605_For the Home") OR level1Cats:("12605_Clothing")
level2Cats:("12605_For the Home_Appliances")
Now if the String is catalog and the array is the one mentioned above then the output should be 12605
.
If its is level1Cats
then the output should be an array of the following strings
12605_For the Home
12605_Clothing
I wrote the following code to implement the above mentioned logic It's doing the functionality without a flaw, Can i implement the same in a different and a more efficient way?
public static String[] parseFQ(String fqField, String fqs[]) {
List<String> prefixes = new ArrayList<String>();
if(fqField != null && fqField.length() > 0
&& fqs != null && fqs.length > 0) {
//Get the fq which starts with given field name.
for(String fq : fqs) {
if(fq.startsWith(fqField)) {
fqField = fq;
break;
}
}
boolean parsed = false;
while(!parsed) {
int quoteStart = fqField.indexOf("\"");
int quoteEnd = (quoteStart >= 0) ? fqField.indexOf("\"", quoteStart+1) : -1;
if(quoteEnd > 0) {
prefixes.add(fqField.substring(quoteStart+1, quoteEnd));
fqField = fqField.substring(quoteEnd+1, fqField.length());
} else {
parsed = true;
}
}
}
//Return the prefixes array
return prefixes.toArray(new String[]{});
}