2012-09-14 5 views
1

나는이 안드로이드 방법 TextUtils.regionMatches 안드로이드 방법 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

감사합니다.

+0

Java 버전에서는 TextUtils.regionMatches가 StringIndexOutOfBoundsException을 throw하는 경우가 있습니다. – ballzak

답변

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); 

경기는 사실이다.

설명 : CharSequence를 하나

toffset에서 시작 (4)의 CharSequence 두에서

ooffset에서 시작 len (6) => QWERTY 동일한 문자의 수를 확보 (8) 및 len (6)과 같은 문자 수 => QWERTY

두 charsequences가 일치하므로이 메서드는 true를 반환합니다.

+0

문자열'one'이 길거나'toffert + len'과 같고'two'도 같으면 매우 좋지만, 알아두면 좋을 것입니다. 그렇지 않으면 오류가 발생합니다. –

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 
}