Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java equivalent for C++'s "std::string::find_first_of"

Tags:

java

android

is there any Java equivalent for C++'s "std::string::find_first_of"?

 string string1( "This is a test string!");
 int location = string1.find_first_of( "aeiou" );
 //location is now "2" (the position of "i")

what is the easiest way to achieve this same functionality?

edit: also the proposed solution have to work for Android.

like image 561
Angel Koh Avatar asked Dec 01 '22 20:12

Angel Koh


1 Answers

Without the use of an external library:

     String string = "This is a test string!";
     String letters = "aeiou";
     Pattern pattern = Pattern.compile("[" + letters + "]");
     Matcher matcher = pattern.matcher(string);
     int position = -1;
     if (matcher.find()) {
         position = matcher.start();
     }
     System.out.println(position); // prints 2
like image 105
jlordo Avatar answered Dec 06 '22 06:12

jlordo