Question

int idx1 = mystr.indexOf("@");

how to add one more character with OR operator. like this

 mystr.indexOf("@" || "#");
Was it helpful?

Solution

try following code:

int minIndex =  Math.min(mystr.indexOf("@") ,mystr.indexOf("#") );

if you don't have one of those char in your String, this return -1.

thanks of Adnan that mention on comment

so you need

int idx1 = mystr.indexOf("@");
int idx2 = mystr.indexOf("#");

int minIndex;
if(idx1 >= 0 && idx2 >= 0 )
 minIndex  =  Math.min(mystr.indexOf("@") ,mystr.indexOf("#") );

else if (idx1 >= 0)
  minIndex = idx1;

else
   minIndex = idx2;

OTHER TIPS

Try this:

int idx1 = (mystr.indexOf("@")==-1)? mystr.indexOf("#"):mystr.indexOf("@");

If case you want the minimum index. You may go for Shayan's code snippet.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top