[英]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.