How to check the charset of string in Java?

后端 未结 5 1724
梦谈多话
梦谈多话 2020-12-07 00:29

In my application I\'m getting the user info from LDAP and sometimes the full username comes in a wrong charset. For example:

ТеÑÑ61 ТеÑÑовиÑ61
         


        
5条回答
  •  Happy的楠姐
    2020-12-07 01:00

    I recommend Apache.tika CharsetDetector, very friendly and strong.

    CharsetDetector detector = new CharsetDetector();
    detector.setText(yourStr.getBytes());
    detector.detect();  // <- return the result, you can check by .getName() method
    

    Further, you can convert any encoded string to your desired one, take utf-8 as example:

    detector.getString(yourStr.getBytes(), "utf-8");
    

提交回复
热议问题