0

Is this the right way to change the charset of a String?

String str = "helloworld你好世界"
String str2 = new String(str.getBytes("GBK"), "UTF-8");
System.out.println(str2);
Walery Strauch
  • 6,792
  • 8
  • 50
  • 57
dvmpr
  • 3
  • 2

2 Answers2

1

You cannot change the "charset" of a java.lang.String object. The "set of characters" that can be used as elements is defined by Unicode. The encoding is fixed as UTF-16.

If you want the sequence of bytes representing the UTF-8 encoding of a java.lang.String s, use

byte[] bytes = s.getBytes( "UTF-8" );

Most of the time, this conversion will be done during I/O, according to your system's default character set anyway.

laune
  • 31,114
  • 3
  • 29
  • 42
0

Charset is just a way to read a string, it's just define the way to convert between string and byte[].

Your code cannot change the charset of the string. Your code just defines a wrong way to read a string.

Daisy Li
  • 54
  • 6