问题
i am running into trouble with JavaScripts strToLower()
var s = 'tür';
alert(s);
alert(s.strToLower());
should result in the same output. however the output is different:
1: tür
2: tã¼r
any suggestions how to handle the uft8-special correctly if using strToLower()?
Martin
回答1:
JavaScript's native .toLowerCase()
method handles UTF-8 just fine:
alert( "tür".toLowerCase() );
Your page encoding might need to be set to UTF-8 with a header or meta
-tag.
回答2:
There is a toLowerCase function and it should work ok as Javascript uses UTF8 internally. Check:
http://jsfiddle.net/5k5hv/
回答3:
You have two built-in functions to convert to lowercase: .toLowerCase() and .toLocaleLowerCase(). Both should work just fine with the example you provide, but you need to make sure you've saved your source code as UTF-8. The exact procedure heavily depends on your IDE or editor.
Additionally, it won't hurt if your web server appends the charset to Content-Type headers.
回答4:
Make sure the file you are working with is encoded into "UTF-8 without BOM" (option in Notepad++) and add <META http-equiv="Content-Type" content="text/html; charset=EUC-JP">
inside <head>
I tried and works.
回答5:
Your code makes no sense. If you assign tür
to a string, then you will always get tür
back. You are probably omitting a step from your code, such as sending the string over AJAX to a server. To do this, the string will have to be encoded, usually using UTF-8, in which case the correct encoding is tür
as you observed. However if the server is working in ISO-8859-1 instead of UTF-8 then it will think that you original string was tür
and will attempt to lowercase it to tã¼r
as described. This will cause problems when the front end tries to interpret it as UTF-8. The solution is to ensure that the server is also working in UTF-8.
来源:https://stackoverflow.com/questions/9990899/javascript-strtolower-uft8-and-german-special-characters