Search code examples
javaandroidobjective-ctranslatecode-translation

rangeOfString and NSMakeRange : from Objective-C to Java


I have this following Objective-C code and I want to translate it into Java code (for Android).

I know I have to use indexOf() but I don't know how to adapt range:NSMakeRange( old_position, ([currentWord length] - old_position) into Java

    NSRange end = [currentWord rangeOfString:@"]"  options:NSCaseInsensitiveSearch range:NSMakeRange( old_position, ([currentWord length] - old_position))];

    if ( end.location != NSNotFound ) {                     
        old_position = end.location + 1;
    }

Solution

  • The rangeOfString:options:range: method is quite similar to the indexOf(str, fromIndex) method.

    Try this:

    Integer start = currentWord.indexOf("[", old_position);
    

    Since the parameter is called fromPosition, you should pass the position from which you want to search, instead of the length of the portion that you want to search.