1

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.

FIG-GHD742
  • 2,486
  • 1
  • 19
  • 27
  • Be wary, the TextUtils.regionMatches throws StringIndexOutOfBoundsException in some cases, which the Java version doesn't. – ballzak Dec 10 '15 at 06:09

2 Answers2

3
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.

Benito Bertoli
  • 25,285
  • 12
  • 54
  • 61
  • Very Good, but also good to know, text if the string `one` is longer or equals with `toffert+len` and same for `two` else you will get a error. – FIG-GHD742 Sep 14 '12 at 12:20
0

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
}
Jacksonkr
  • 31,583
  • 39
  • 180
  • 284