简体   繁体   English

如何比较两个Java字符串而不必担心Parse查询中的任何一个的大小写?

[英]How can I compare two Java strings without having to worry about case for either one in Parse queries?

I'm making a program where a user searches for items based on item name or description. 我正在编写一个程序,其中用户根据项目名称或描述搜索项目。 I'm using query.whereContains(key, value) since I want it to check if either contains the keyword they're looking for. 我正在使用query.whereContains(key,value),因为我要它检查其中是否包含他们要查找的关键字。 My question is how can I compare the keyword(the value) to the item name or description but regardless of case. 我的问题是如何将关键字(值)与商品名称或说明进行比较,而不管大小写如何。 I know Java's equalsignoreCase() exists, but that returns a boolean which doesn't match the whereContains() method's paramters. 我知道Java的equalsignoreCase()存在,但是返回的布尔值与whereContains()方法的参数不匹配。

Example of what I mean: 我的意思示例:

If the user searches for "eXAmplE", which will be the keyword, and a description of an item in the cloud is "this is an ExAMplE" exists, the query should include that. 如果用户搜索“ eXAmplE”(将作为关键字),并且云中的项目描述为“ this is ExAMplE”,则查询应包括该内容。 How would I go about implementing this? 我将如何实施呢?

by normalizing the case of all Strings involved before comparing them. 通过在比较它们之前标准化所有涉及的字符串的大小写。

something like this: 像这样的东西:

String original = "SoMeCrazyCASE";
String lowerCased = original.toLowerCase(Locale.ROOT);
//now work with the lowercased one

note - its important to lower case in the root locale to avoid issues with the current language on the machine youre running on 注意-在根语言环境中小写很重要,以避免运行您的计算机上的当前语言出现问题

original.equalsIgnoreCase(other)

这可能比执行s.toLowerCase()更快,因为不需要创建额外的对象。

Try the following: 请尝试以下操作:

if(myDescription.toLowerCase().contains((keyword.toLowerCase())){
    //do stuff here
}

More complex queries may need you to implement the search using patterns. 更复杂的查询可能需要您使用模式来实现搜索。

Hope it helps. 希望能帮助到你。

Try this 尝试这个

 String str1 = "hello";
    String str2 = "HELLO";
    if(str1.equalsIgnoreCase(str2)){
        //YOUR LOGIc
    }

Try this. 尝试这个。

String str1="HAI";
String str2="hai";
if(str1.equalsIgnoreCase(str2))
{
    //Use your code
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我想要一个我可以在自己程序的时间内读取的HttpResponse对象(或类似对象),而不必担心服务器超时 - I want an HttpResponse object (or similar) that I can read in my own program's time without having to worry about server timeouts 如何比较两个字符串并将不同的项目存储在Java中? - How I can to compare two strings and store the different items in Java? 如何在Java中比较一个数组中的两个整数? - How can I compare two integers in one array in Java? 如何在不考虑空格和Java字符串大小写的情况下比较两个字符串? - How compare two string without considering spaces and case of string in java? 如何在不考虑空格的情况下比较java中的两个字符串? - How to compare two Strings in java without considering spaces? 如何在Java中比较两个字符串? - how to compare two strings in java? 如何在 Java 的同一行中将两个或一个整数作为输入? - How can I take either two or one integer as input on the same line in Java? 如何比较 java 中的两个字符串并按字母顺序定义它们中的哪个比另一个小? - How can I compare two strings in java and define which of them is smaller than the other alphabetically? 在 Java 中,如何将 JSON 或 XML 字符串映射到同一个 POJO,但 XML 具有与 JSON 不同的字段/属性名称? - In Java, how can I map a string that is either JSON or XML to the same POJO, but with the XML having one different field/attribute name from the JSON? 我如何比较Java中的两个小时? - How can I compare two hours in java?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM