No. | 方法名称 | 类型 | 描述 |
1 | public String (byte[ ]bytes) | 构造 | 将全部字节数组变为字符串 |
2 | public String(byte[ ]bytes ,int offset,int length) | 构造 | 将部分字节数组变为字符串 |
3 | public byte[ ]getBytes() | 普通 | 将字符串变为字节数组 |
4 | public byte[ ]getBytes(String charsetName)throws UnsupportedEncodingException | 普通 | 进行编码转换 |
public class Demo {
public static void main(String[] args) {
String str = "helloworld";
byte[] data = str.getBytes();
for (int i = 0; i < data.length; i++) {
data[i] -= 32;// 转换成大写
}
System.out.println(new String(data));// 全部转换
System.out.println(new String(data, 5, 5));// 部分转换
}
}
