我有两个String s,str1和str2。 如何检查str1是否包含在str1中,忽略大小写?

indexOf和contains都是逐个字符的,所以如果你需要更快的字符串搜索(你可以得到),那么你需要实现许多已发布的算法之一。

我有同样的问题,这里是答案:) stackoverflow.com/a/86832/621951

str1.toLowerCase().contains(str2.toLowerCase())

出于某种原因,当我打电话给"2014-03-25T17:55:00"时,我会变得虚假.contains("T")

是否有返回的函数和包含(str2)的位置?

@RadijatoR是的,它被称为indexOf,如int pos = str1.indexOf(str2)或不区分大小写int pos = str1.toLowerCase().indexOf(str2.toLowerCase())

似乎现在正在工作。不知道之前发生了什么。相同的代码。

@frostymarvelous我有同样的问题,java中的错误...我试图将"成功"与"成功"匹配.toLowerCase(),将失败,将其更改为"ccess"然后它起作用。很奇怪。奇怪的是,匹配"SUCCESS"和"Successful".toUpperCase()也有效

@Fonix是str1,是str2?
@frostymarvelous"成功"是str 1,所以它是"Successful".toLowerCase().contains("success");("成功"是我从服务器的json响应中得到的变量)
@JeremyList ideone.com/weYI1e打印true。
@JeremyList它打印为true
matches()怎么样?
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.*");

您的"indexOf"示例应使用> = 0,而不是> 0,因为如果子字符串出现在字符串的开头,则0有效。 (不是在你的例子中,但在其他情况下可以。)添加了这个响应,因为人们显然仍在搜索并找到这个答案。

如果你能够使用org.apache.commons.lang.StringUtils,我建议使用以下内容:

String container ="aBcDeFg";
String content ="dE";
boolean containerContainsContent = StringUtils.containsIgnoreCase(container, content);

您可以使用toLowerCase()方法:

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 +"." );
}

请注意,通过创建自己的方法,您可以重用它。然后,当有人指出你应该使用contains而不是indexOf时,你只需要改变一行代码。

记得在传递null对象时添加有关行为的Javadoc。

解决方案帮助:)

我也赞成RegEx解决方案。代码会更清晰。在我知道字符串变得很大的情况下,我会毫不犹豫地使用toLowerCase(),因为字符串是不可变的并且必须被复制。此外,matches()解决方案可能会令人困惑,因为它将正则表达式作为参数(搜索"Need $ 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 +"." );
}

(注意:根据您的需要,您可能希望以不同的方式处理NULL和空字符串。我认为我的方式更接近字符串的Java规范。)

速度关键解决方案可以包括通过字符迭代干草堆字符寻找针的第一个字符。当第一个字符匹配时(不区分大小写),开始逐个字符地迭代针,在大海捞针中查找相应的字符,如果所有字符都匹配则返回"true"。如果遇到不匹配的字符,则通过下一个字符的haystack继续迭代,如果达到position> haystack.length() - needle.length()则返回"false"。

我愿意:Pattern.CASE_INSENSITIVE|Pattern.LITERAL

@mikejones我怎样才能检查这个单词?考虑下面的情况1)"这是一个可能需要$ le的大海捞针。"; 2)"这是一个大海捞针,可能需要少量的东西。";我只希望案例1匹配,因为在这种情况下需要$ le作为单词出现。我不希望第二种情况匹配。我怎样才能做到这一点?

我将使用contains方法和toUpper方法的组合,它们是String类的一部分。一个例子如下:

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
Search2=false

不行。转换为大写/小写时,一些奇怪的国际字符会转换为多个字符。例如:"ß".toUpperCase().equals("SS")

那就好了。这就是用德语写双s的方式。