Utf-8 string comparison in java

In my java program, I am extracting some data from xml. This xml has several international characters and is encoded in utf8. Now I read this xml using the xml parser. As soon as I get a specific international string from an XML parser, I need to compare it with a set of predefined strings. The problem is that I am using string.equals to compare inside strings.

How to compare strings with international strings in java? I use SAXParser and XMLReader to read strings from xml.

Here is a string that compares strings

 String country;
 country = getXMLNodeString();

 if(country.equals("Côte d'Ivoire"))
 {    

 } 

  getXMLNodeString()
  {

  /* Get a SAXParser from the SAXPArserFactory. */  
        SAXParserFactory spf = SAXParserFactory.newInstance();
        SAXParser sp = spf.newSAXParser();

        /* Get the XMLReader of the SAXParser we created. */
        XMLReader xr = sp.getXMLReader();
        /* Create a new ContentHandler and apply it to the XML-Reader*/
        XmlParser xmlParser = new XmlParser();  //my class to parse xml
        xr.setContentHandler(xmlParser);  

        /* Parse the xml-data from our URL. */
        xr.parse(new InputSource(url.openStream()));
        /* Parsing has finished. */


       //return string here
  }
+5
source share
5 answers

Java String char s, 16- . Unicode, 64K .

String "Côte d'Ivoire" . XML- , String . :

  • XML ;

  • .

, XML US-ASCII UTF-8. , . , , , . UTF8 String , XML:

byte[] bytes = "Côte d'Ivoire".getBytes("UTF-8");

, " ". , 64K ( " " Unicode). . Java. , , .

+6

, , , javac. , -encoding javac.

"" .

, Java, XML-.

+3

Java UTF-16. XML- UTF-8 UTF-16 , UTF-16 , equals(). , , , , , - .

+2

XML UTF-8, contentEquals ( ) :

if (strMyvalue.contentEquals("Côte d'Ivoire") {
    // execute
}
0

, , . , ? .

0

All Articles