字符串怎么解决乱码

如果字符串出现乱码:

         String name = "放假";
        //1.1 先将字符串转换成ISO-8859-1 , 然后利用String的构造函数将字符串转换成UTF-8  new String(Bytes[] bytes , “UTF-8”)
        //将name变量的值转换成字节组,默认转换成ISO-8859-1编码
        byte[] bytes = name.getBytes();
        //将name从ISO-8859-1转换成UTF-8
        name = new String(bytes,"UTF-8");
        //1.2 直接将字符串转换成UTF-8
        byte[] bytes1 = name.getBytes("UTF-8");
        name = new String(bytes1);
         //1.3 UrlEncoder的encoder(字符串,字符码)设置编码 , UrlDecoder的decoder(字符串,字符码)解密,然后设置指定的字符码
        String nameEncode = URLEncoder.encode(name, "ISO-8859-1");
        nameEncode = URLDecoder.decode(nameEncode, "UTF-8");

你可能感兴趣的:(java,开发语言,后端)