Вопрос

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

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

 mystr.indexOf("@" || "#");
Это было полезно?

Решение

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;

Другие советы

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.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top