java.lang.String.getBytes()%uA0方法将此String解码使用平台的默认字符集的字节序列,并将结果存储到一个新的字节数组。
声明
以下是java.lang.String.getBytes()方法的声明
public byte[] getBytes()
参数
-
NA
返回值
此方法返回得到的字节数组。
异常
-
NA
例子
下面的例子显示java.lang.String.getBytes()方法的使用。
package com.yiibai import java.lang.* public class StringDemo { public static void main(String[] args) { String str1 = "tutorials point" // copy contents of str1 to a byte array. byte[] val = str1.getBytes() // create string str1 using the contents of the byte array String str2 = new String(val) // prints the byte array. System.out.println("String &apos" + str2 + "&apos is equal to str1") } }
让我们来编译和运行上面的程序,这将产生以下结果:
String &apostutorials point&apos is equal to str1