Looking for punctuations marks in a string and then finding their index to do a substring

Viewed 651

I have some string where I need to extract a substring from it based on either the first occurrence of a punctuation mark or the first occurrence of a digit. E.g

from Taltz 80mg autoinjector I need to extract Taltz or from Trulicity 0.75mg, weekly I need to extract Trulicity

Here's 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?

1 Answers
Related