String
String url = "http://www.baidu.com";
String webName = "百度";示例:
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
public class Main {
public static void main(String[] args) {
try {
System.out.println("-------------------------------- testStringConstructors -----------------------");
String str01 = new String();
String str02 = new String("String02");
String str03 = new String(new char[]{'s','t','r','0','3'});
String str04 = new String(new char[]{'s','t','r','0','4'}, 1, 3); // 1表示起始位置,3表示个数
String str05 = new String(new byte[]{0x61, 0x62, 0x63, 0x64, 0x65}); // 0x61在ASC表中,对应字符"a"; 1表示起始位置,3表示长度
String str06 = new String(new byte[]{0x61, 0x62, 0x63, 0x64, 0x65}, 1, 3); // 0x61在ASC表中,对应字符"a"; 1表示起始位置,3表示长度
String str07 = new String(new byte[]{0x61, 0x62, 0x63, 0x64, 0x65}, 0); // 0x61在ASC表中,对应字符"a";0,表示“高字节”
String str08 = new String(new byte[]{0x61, 0x62, 0x63, 0x64, 0x65}, 0, 1, 3); // 0x61在ASC表中,对应字符"a"; 0,表示“高字节”;1表示起始位置,3表示长度
String str09 = new String(new byte[]{(byte)0xe5, (byte)0xad, (byte)0x97, /* 字-对应的utf-8编码 */
(byte)0xe7, (byte)0xac, (byte)0xa6, /* 符-对应的utf-8编码 */
(byte)0xe7, (byte)0xbc, (byte)0x96, /* 编-对应的utf-8编码 */
(byte)0xe7, (byte)0xa0, (byte)0x81, /* 码-对应的utf-8编码 */ },
0, 12, "utf-8"); // 0表示起始位置,12表示长度。
String str10 = new String(new byte[]{(byte)0x5b, (byte)0x57, /* 字-对应的utf-16编码 */
(byte)0x7b, (byte)0x26, /* 符-对应的utf-16编码 */
(byte)0x7f, (byte)0x16, /* 编-对应的utf-16编码 */
(byte)0x78, (byte)0x01, /* 码-对应的utf-16编码 */ },
0, 8, "utf-16"); // 0表示起始位置,8表示长度。
String str11 = new String(new byte[]{(byte)0xd7, (byte)0xd6, /* 字-对应的gb2312编码 */
(byte)0xb7, (byte)0xfb, /* 符-对应的gb2312编码 */
(byte)0xb1, (byte)0xe0, /* 编-对应的gb2312编码 */
(byte)0xc2, (byte)0xeb, /* 码-对应的gb2312编码 */ },
Charset.forName("gb2312"));
String str12 = new String(new byte[]{(byte)0xd7, (byte)0xd6, /* 字-对应的gbk编码 */
(byte)0xb7, (byte)0xfb, /* 符-对应的gbk编码 */
(byte)0xb1, (byte)0xe0, /* 编-对应的gbk编码 */
(byte)0xc2, (byte)0xeb, /* 码-对应的gbk编码 */ },
0, 8, Charset.forName("gbk"));
String str13 = new String(new int[] {0x5b57, 0x7b26, 0x7f16, 0x7801}, 0, 4); // "字符编码"(\u5b57是‘字’的unicode编码)。0表示起始位置,4表示长度。
String str14 = new String(new StringBuffer("StringBuffer"));
String str15 = new String(new StringBuilder("StringBuilder"));
System.out.printf(" str01=%s \n str02=%s \n str03=%s \n str04=%s \n str05=%s \n str06=%s \n str07=%s \n str08=%s\n str09=%s\n str10=%s\n str11=%s\n str12=%s\n str13=%s\n str14=%s\n str15=%s\n",
str01, str02, str03, str04, str05, str06, str07, str08, str09, str10, str11, str12, str13, str14, str15);
System.out.println();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
}Last updated