5

Currently i am working in simple android app, I have tried to range of string in iphone, its works, but i didn't know this in Android app? In iphone:

result = "mobile - check balance | rate: 0.036"

if ( [result rangeOfString:@"rate:"].location != NSNotFound) 
{

}

How to convert this in Android, please help me

5 Answers 5

6

Have you tried something like result = "mobile- check balance | rate: 0.036"

if( result.indexOf("rate")!=-1)
{}

I've been using objective-c recently too, and I pulled this from here

3
  • i have one doubt, can u help me? Commented Aug 16, 2012 at 13:05
  • In iphone - if ([result hasPrefix:@"rate:"]){ }, how to conver this in android? Commented Aug 16, 2012 at 13:05
  • id(result.startsWith("rate:"))
    – CBredlow
    Commented Aug 16, 2012 at 13:17
2

try this

    String strVowels = "mobile - check balance | rate: 0.036";
    if (strVowels.contains("rate:") == true) {
        // String contains IOU sub-string!
        Log.i("mylog", "this word is in string.");
    } else {
        Log.i("mylog", "this word is not in string.");
    }
0
1

I wonder if you mean String.contains()?

0
0

Your rangeOfString usage is roughly equivalent to String#indexOf()

String result = "mobile - check balance | rate: 0.036";

if (result.indexOf("rate:") != -1) 
{

}

but you can write this more clearly in Java with String#contains():

if (result.contains("rate:")) 
{

}
0
0

You would need to use String.indexOf and then combine the value returned with the length of the String you're looking for.

The indexOf function returns the index of the first letter of the string you passed in so that can be treated as your "zero" position.

Alternatively, as Matt Ball points out, the way you're using it, the String.contains method would be just as effective, if not slightly cleaner.

2
  • result =@"Error: access denied" In iphone - if ([result hasPrefix:@"Error:"]){ }, how to conver this in android? please help me Commented Aug 16, 2012 at 13:11
  • result = "Error: access denied"; if(result.startsWith("Error:")){} Commented Aug 16, 2012 at 22:35

Not the answer you're looking for? Browse other questions tagged or ask your own question.