Java String getBytes()
方法使用指定的字元集將此String編碼為位元組序列,並將結果存儲到新的位元組數組中。
語法
以下是此方法的語法 -
public byte[] getBytes(String charsetName) throws UnsupportedEncodingException
參數
charsetName
- 字元集的名稱
返回值
- 返回結果為位元組數組。
示例
import java.io.*;
public class Test {
public static void main(String args[]) {
String Str1 = new String("Welcome to xuhuhu.com");
try {
String Str2 = new String( Str1.getBytes( "UTF-8" ));
System.out.println("Returned Value " + Str2 );
Str2 = new String (Str1.getBytes( "ISO-8859-1" ));
System.out.println("Returned Value " + Str2 );
} catch ( UnsupportedEncodingException e) {
System.out.println("Unsupported character set");
}
}
}
執行上面示例代碼,得到以下結果:
Returned Value Welcome to xuhuhu.com
Returned Value Welcome to xuhuhu.com
上一篇:
java中方法重載和方法重寫的區別
下一篇:無