简体   繁体   中英

toLowerCase with special/unicode characters throws exception

correct me if I'm wrong.

If str has a character such as " " in it then running:

str.toLowerCase(Locale.English);

throws a null pointer exception. That's the behavior I'm seeing.

So what's the deal here? What's going on? It isn't specified that toLowerCase throws a null pointer exception.

Is there an easy way to get around this? I need the str to be lower case to be able to use a case insensitive contains check with another string, but I need the string to contain those characters for it to be displayed correctly.

What would you say is the most efficient solution if there is no "easy way?"

Rather than converting to lower case, I would suggest trying the String object's equalsIgnoreCase() method .

Although, as the comments do say, there is no reason for the toLowerCase() function to be throwing a null pointer exception unless a parameter is null. I would either use a debugger to check the state of str or wrap the toLowerCase() function call in an if statement and if str is null, do something (like throw an exception, print a statement, log a message) and not execute the function call.

Hmm, it works for me:

String upper = new String("asdCSD4rSDFSDFS•••••••••XCj");
String lower = upper.toLowerCase(Locale.ENGLISH);
System.out.println("Lower " + lower);

Lower asdcsd4rsdfsdfs•••••••••xcj

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM