Android方法TextUtils.regionMatches

时间:2012-09-14 11:18:00

标签: android charsequence textutils

我找到了这个android方法TextUtils.regionMatches

但由于某种原因,所以不清楚这个功能是如何运作的。

这个功能可以在这里找到: http://developer.android.com/reference/android/text/TextUtils.html#regionMatches%28java.lang.CharSequence,%20int,%20java.lang.CharSequence,%20int,%20int%29

此方法的基本代码, http://androidxref.com/4.1.1/xref/frameworks/base/core/java/android/text/TextUtils.java#220

感谢那些可能会对如何调用该函数有所了解的人。

2 个答案:

答案 0 :(得分:3)

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

示例代码:

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

匹配是真的。

说明:

在第一个问题中,从toffset(4)开始,得到的数字等于len(6)=> QWERTY

在第二个问题中,从ooffset(8)开始,得到的数字等于len(6)=> QWERTY

两个charsequences匹配,因此该方法返回true。

答案 1 :(得分:0)

我刚写了这个来检查" http"在字符串的第一个,另一个例子总是帮助访问者。

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
}