저도 두 'String str2 str1' 와 ',' 의 '. How do I check '가 포함된', '만약' str2 str1 대소문자 무시?
' ()' 일치시킵니다 어때?
String string = "Madam, I am Adam";
// Starts with
boolean b = string.startsWith("Mad"); // true
// Ends with
b = string.endsWith("dam"); // true
// Anywhere
b = string.indexOf("I am") >= 0; // true
// To ignore case, regular expressions must be used
// Starts with
b = string.matches("(?i)mad.*");
// Ends with
b = string.matches("(?i).*adam");
// Anywhere
b = string.matches("(?i).*i am.*");
'토로르카스 ()' 방법을 사용할 수 있습니다.
public boolean contains( String haystack, String needle ) {
haystack = haystack == null ? "" : haystack;
needle = needle == null ? "" : needle;
// Works, but is not the best.
//return haystack.toLowerCase().indexOf( needle.toLowerCase() ) > -1
return haystack.toLowerCase().contains( needle.toLowerCase() )
}
그럼 부르네요 사용:
if( contains( str1, str2 ) ) {
System.out.println( "Found " + str2 + " within " + str1 + "." );
}
자신만의 방법을 만들어 재사용합니다 것을 알 수 있습니다. 그런 점을 지적하고, 누군가 대신 콩지름에 '' 한 줄의 코드가 포함되어 있을 수 있습니다 ',' indexOf 변경될 수 있습니다.
난 또 응답이 RegEx 솔루션입니다. 이 코드는 훨씬 더 좋은 것입니다. 꼭 이래야겠어요 소개하십시오 토로르카스 상황에서 사용할 수 있기 때문에 목적지를 문장열 알았다 () 는 대형 문장열 불변 및 있을 것이라고 복사됩니다. 또 it takes a 매치 () 솔루션을 복잡할 수 있기 때문에 정규식이 인수로 (검색, $ le" " 필요합니다. 차가운 일으킬 수).
일부 건물 위의 예제:
public boolean containsIgnoreCase( String haystack, String needle ) {
if(needle.equals(""))
return true;
if(haystack == null || needle == null || haystack .equals(""))
return false;
Pattern p = Pattern.compile(needle,Pattern.CASE_INSENSITIVE+Pattern.LITERAL);
Matcher m = p.matcher(haystack);
return m.find();
}
example call:
String needle = "Need$le";
String haystack = "This is a haystack that might have a need$le in it.";
if( containsIgnoreCase( haystack, needle) ) {
System.out.println( "Found " + needle + " within " + haystack + "." );
}
(참고: 처리하십니까 nulll 및 빈 문자열을 필요에 따라 다르게 할 수 있습니다. 내 생각엔 운행에서어떠한 가져왔소 가깝다 위해 spec jave 써줬지)
핵심 솔루션을 속도용 전체 반복을 통해 찾고 있을 수 있습니다 의 첫 번째 문자는 문자 짚단 문자 이글라. 처음 시작할 때 문자 일치됩니다 (경우 이젠슈타이블리), 문자, 전체 반복을 통해 해당 문자를 짚단 바늘 찾는 문자 및 반납 " true"; 만약 모든 문자를 프레젠테이션이든 일치시킵니다. 비사양 매칭됨 문자 발견될 경우, 다시 시작할 때, 그 다음 문자를 통해 이터레이션에 짚단 " false". 만약 > 포지셔닝하십시오. 네들레그렌크스 헤이스트랙.렌크스 () - () 는 도달했음.
String string1 = "AAABBBCCC";
String string2 = "DDDEEEFFF";
String searchForThis = "AABB";
System.out.println("Search1="+string1.toUpperCase().contains(searchForThis.toUpperCase()));
System.out.println("Search2="+string2.toUpperCase().contains(searchForThis.toUpperCase()));
이 반환되므로: > Search1 = true< br>;; Search2 = false