Check if a String contains encoded characters

前端 未结 6 1045
情深已故
情深已故 2020-12-18 08:46

Hello I am looking for a way to detect if a string has being encoded

For example

    String name = \"Hellä world\";
    String encoded = new String(n         


        
6条回答
  •  醉酒成梦
    2020-12-18 09:19

    Sounds like you want to check if a string that was decoded from bytes in latin1 could have been decoded in UTF-8, too. That's easy because illegal byte sequences are replaced by the character \ufffd:

    String recoded = new String(encoded.getBytes("iso-8859-1"), "UTF-8");
    return recoded.indexOf('\uFFFD') == -1; // No replacement character found
    

提交回复
热议问题