سؤال

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