为了账号安全,请及时绑定邮箱和手机立即绑定

您好,关于java string 的charsetname的具体问题如下:

您好,关于java string 的charsetname的具体问题如下:

红糖糍粑 2021-11-01 10:06:42
public class NewClass {public static void main(String[] args){byte[] b={1,2,3};String str = new String(b,2,3,UTF-16);System.out.println(str);}}//sdk java1.6 , 为什麼error//Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - 找不到符号符号: 变量 UTF
查看完整描述

2 回答

?
慕侠2389804

TA贡献1719条经验 获得超6个赞

先加
import java.nio.charset.Charset;
String str = new String(b,2,3,UTF-16);
改为
String str = new String(b,2,3,Charset.forName("UTF-16"));
这个地方需要的是Charset类...

查看完整回答
反对 回复 2021-11-06
?
梦里花落0921

TA贡献1772条经验 获得超5个赞

public class NewClass {
public static void main(String[] args)
{
byte[] b={1,2,3};
String str = new String(b,2,3,UTF-16) // 这里会下标越界;
System.out.println(str);
}
}
把这段代码改成:
import java.io.*;
public class NewClass {
public static void main(String[] args){
try{
byte[] b={1,2,3};
String str = new String(b,1,2,"UTF-16");// 这里会抛出一个i里的异常
System.out.println(str);
}catch(UnsupportedEncodingException e){
e.printStackTrace();
}
}
}



查看完整回答
反对 回复 2021-11-06
  • 2 回答
  • 0 关注
  • 296 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信