当前位置: 动力学知识库 > 问答 > 编程问答 >

Java String.contains() not working on a cyrillic string

问题描述:

Here is what happens.

User types in "лос ан"

I have a bunch of products whose location is "лос анджелис"

if I do:

String userInput = "лос ан"

for(Product product : products) {

if(product.getCity().trim().toLowerCase().contains(userInput.trim().toLowerCase())) {

System.out.println("MATCH");

}

}

I don't get MATCH.

This works for Latin characters

网友答案:

try specifying Locale in toLowerCase() on both sides of the equation: http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#toLowerCase(java.util.Locale)

网友答案:

The editor and the compiler (javac -encoding) must use the same encoding.

The compiler encoding are done easily. The editor, source encoding, can be tested with a programmer's editor like NotePad++ or JEdit, which can switch encodings.

You can also u-escape the Java source text to check this:

String userInput = "\u043b\u043e\u0441 \u0430\u043d";

If that does not work, there is a discrepancy between the encodings.

Furthermore String.toLowerCase(new Locale("ru", "RU")) or such is already mentioned.

网友答案:

Using jdk 1.8.0_45, the following code gives a match in both cases:

System.out.println("лос анджелис".trim().toLowerCase().contains("лос ан".trim().toLowerCase()));
System.out.println("лос анджелис".trim().toLowerCase(Locale.ROOT).contains("лос ан".trim().toLowerCase(Locale.ROOT)));

As others already mentioned, you may look for a working Locale as argument to String#toLowerCase.

分享给朋友:
您可能感兴趣的文章:
随机阅读: