字节一般用来数据的传输或者进行编码转换,方法如下:
| public String (byte[] bytes){构造方法} | 将全部字节数组变为字符串 |
|---|---|
| public String (byte[] bytes,int offset,int length){构造方法} | 将部分字节数组变为字符串 |
| public byte[] getBytes(){普通方法} | 将字符串转为字节数组 |
| public byte[] getBytes(String charsetName)throws UnsupportedEncondingException{普通方法} | 编码转换 |
范例:字符串与字节转换
public class StringDemo148 {
public static void main(String[] args) {
String name="qwertyui";
byte [] arr=name.getBytes();
for(int x=0;x<arr.length;x++) {
arr[x]-=32;
}
String newName=new String(arr);
System.out.println(newName);
System.out.println(new String(arr,0,4));
}
}
QWERTYUI
QWER
本文详细介绍字节与字符串之间的转换方法,包括使用构造方法将字节数组转化为字符串,以及如何通过getBytes方法将字符串转化为字节数组。通过实例演示了编码转换的过程,展示了如何在Java中操作字节与字符串。
1066

被折叠的 条评论
为什么被折叠?



