Question

I has found this android method TextUtils.regionMatches

But for some reason, so it is not clear how this function works.

The function can be found here: http://developer.android.com/reference/android/text/TextUtils.html#regionMatches%28java.lang.CharSequence,%20int,%20java.lang.CharSequence,%20int,%20int%29

And the base code for this method here, http://androidxref.com/4.1.1/xref/frameworks/base/core/java/android/text/TextUtils.java#220

Thanks for those who might shed some light on how the function is called.

Was it helpful?

Solution

public static boolean regionMatches (CharSequence one,
                   int toffset, CharSequence two, int ooffset, int len)

Sample code:

CharSequence one = "asdfQWERTYc1234";
CharSequence two = "ghjklzxcQWERTYg7890kl";
boolean match = TextUtils.regionMatches(one, 4, two, 8, 6);

match is true.

Explanation:

In charsequence one, start from toffset (4) and get a number of characters equal to len (6) => QWERTY

In charsequence two, start from ooffset (8) and get a number of characters equal to len (6) => QWERTY

Both charsequences match, so the method returns true.

OTHER TIPS

I just wrote this to check for "http" at the very first of a string and another example always helps visitors.

url = "url.without/protocol.info"; // will match
// url = "http://url.with/protocol.info"; // won't match

String match = "http";
if(!url.regionMatches(true, 0, match, 0, match.length())) {
    //do something
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top