I have a line where I need to extract a substring from it, based either on the first introduction of the punctuation mark, or on the first occurrence of a digit. for instance
from Taltz 80mg autoinjectorI need to extract Taltzor from Trulicity 0.75mg, weeklyI need to extractTrulicity
Here is my code:
char [] punctuations = {'.' , ',' , ';' , ':','"' , '\'' ,'/', ')' , '('};
String value = "Taltz, 80mg autoinjector";
int pos = value.replaceFirst("^(\\D+).*$", "$1").length();
for(int j = 0; j < value.length(); j++) {
for (int k = 0; k < punctuations.length;k++){
if(value.charAt(j) == punctuations[k]){
value = value.substring(0,value.indexOf(punctuations[k]));
break;
}
}
}
if(value.matches(".*\\d+.*")){
value = value.substring(0, pos);
}
System.out.println(value);
}
Is there a more efficient way to do this?
source
share