How to do string comparison?
I want to do string comparison in Java and MySQL for Utf-8. How to do that I am confused. How to do that. Any suggestions please. Is it normal string c开发者_C百科omparison or anything special.
Java Strings are composed of Unicode UTF-16 characters.
There are Charset classes to manage conversion to and from other character sets. In the case of UTF-8, a subset of UTF-16, conversion to UTF-16 should not be problematic - by the time you have a String the conversion with have happened, then all normal String operations apply.
See http://java.sun.com/javase/technologies/core/basic/intl/faq.jsp
This StackOverflow Question addresses the use of UTF-8 in Java/MYSQL.
The obvious approach is to just compare the strings using Java String.equals(String)
etc. Java Strings are Unicode Strings*, and Java and MySQL both know how to handle Unicode data.
The only tricky bit is making sure that you've configured MySQL to use Unicode (typically UTF-8). In older versions of MySQL this typically entailed defining the database appropriately AND using certain parameters in the JDBC URL. Either way, consult the MySQL and MySQL connector documentation for the versions you are using.
* Strictly speaking, Java Strings are encoded in a form of UTF-16, with Unicode codepoints beyond 65535 represented as a pair of char
values.
精彩评论