String 构造函数
来源:互联网 发布:柯南 知乎 编辑:程序博客网 时间:2024/06/14 23:33
1 /** 2 * String 构造函数演示程序 3 * 4 * @author skywang 5 */ 6 import java.nio.charset.Charset; 7 import java.io.UnsupportedEncodingException; 8 9 public class StringContructorTest {10 11 public static void main(String[] args) {12 testStringConstructors() ;13 }14 15 /**16 * String 构造函数测试程序17 */18 private static void testStringConstructors() {19 try {20 System.out.println("-------------------------------- testStringConstructors -----------------------");21 22 String str01 = new String();23 String str02 = new String("String02");24 String str03 = new String(new char[]{'s','t','r','0','3'});25 String str04 = new String(new char[]{'s','t','r','0','4'}, 1, 3); // 1表示起始位置,3表示个数26 String str05 = new String(new byte[]{0x61, 0x62, 0x63, 0x64, 0x65}); // 0x61在ASC表中,对应字符"a"; 1表示起始位置,3表示长度27 String str06 = new String(new byte[]{0x61, 0x62, 0x63, 0x64, 0x65}, 1, 3); // 0x61在ASC表中,对应字符"a"; 1表示起始位置,3表示长度28 String str07 = new String(new byte[]{0x61, 0x62, 0x63, 0x64, 0x65}, 0); // 0x61在ASC表中,对应字符"a";0,表示“高字节”29 String str08 = new String(new byte[]{0x61, 0x62, 0x63, 0x64, 0x65}, 0, 1, 3); // 0x61在ASC表中,对应字符"a"; 0,表示“高字节”;1表示起始位置,3表示长度30 String str09 = new String(new byte[]{(byte)0xe5, (byte)0xad, (byte)0x97, /* 字-对应的utf-8编码 */ 31 (byte)0xe7, (byte)0xac, (byte)0xa6, /* 符-对应的utf-8编码 */ 32 (byte)0xe7, (byte)0xbc, (byte)0x96, /* 编-对应的utf-8编码 */ 33 (byte)0xe7, (byte)0xa0, (byte)0x81, /* 码-对应的utf-8编码 */ }, 34 0, 12, "utf-8"); // 0表示起始位置,12表示长度。35 String str10 = new String(new byte[]{(byte)0x5b, (byte)0x57, /* 字-对应的utf-16编码 */ 36 (byte)0x7b, (byte)0x26, /* 符-对应的utf-16编码 */ 37 (byte)0x7f, (byte)0x16, /* 编-对应的utf-16编码 */ 38 (byte)0x78, (byte)0x01, /* 码-对应的utf-16编码 */ }, 39 0, 8, "utf-16"); // 0表示起始位置,8表示长度。40 String str11 = new String(new byte[]{(byte)0xd7, (byte)0xd6, /* 字-对应的gb2312编码 */ 41 (byte)0xb7, (byte)0xfb, /* 符-对应的gb2312编码 */ 42 (byte)0xb1, (byte)0xe0, /* 编-对应的gb2312编码 */ 43 (byte)0xc2, (byte)0xeb, /* 码-对应的gb2312编码 */ }, 44 Charset.forName("gb2312")); 45 String str12 = new String(new byte[]{(byte)0xd7, (byte)0xd6, /* 字-对应的gbk编码 */ 46 (byte)0xb7, (byte)0xfb, /* 符-对应的gbk编码 */ 47 (byte)0xb1, (byte)0xe0, /* 编-对应的gbk编码 */ 48 (byte)0xc2, (byte)0xeb, /* 码-对应的gbk编码 */ }, 49 0, 8, Charset.forName("gbk")); 50 String str13 = new String(new int[] {0x5b57, 0x7b26, 0x7f16, 0x7801}, 0, 4); // "字符编码"(\u5b57是‘字’的unicode编码)。0表示起始位置,4表示长度。51 String str14 = new String(new StringBuffer("StringBuffer"));52 String str15 = new String(new StringBuilder("StringBuilder"));53 54 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",55 str01, str02, str03, str04, str05, str06, str07, str08, str09, str10, str11, str12, str13, str14, str15);56 57 58 System.out.println();59 } catch (UnsupportedEncodingException e) {60 e.printStackTrace();61 }62 }63 }
1.理解byte类型,char类型
2.java中char类型使用的是union编码方式
3. new String(new int[]..)构造方法中传参是int类型,其编码是union方式,(union编码占用16位,所以不用8位的byte形式然后再指定编码类型)
new String(new byte[] 构造方法中传参是byte,具体编码方式需要指定
阅读全文
0 0
- Exception 构造函数 (String)
- string构造函数实现
- string 的构造函数
- String 类构造函数
- string构造函数原理
- C++String构造函数
- c++ string 构造函数
- string 构造函数 java
- String构造函数实现
- String 构造函数
- String 构造函数
- String构造函数
- String类:构造函数,拷贝构造函数
- String的构造函数拷贝构造函数
- String 构造,析构,复制,拷贝构造函数
- std::string 的构造函数
- string类的构造函数
- C++ 的 string构造函数
- Android常用开源项目(二十五)
- Hive常见问题及解决方法
- Spring事务机制中@Transactional失效的原因
- eclipse svn ignore设置无效,查看历史记录无效
- .NET是什么
- String 构造函数
- Guideline 5.1.1
- Codeforces Vasya the Hipster
- 【单例模式】的多种实现方式
- Java连接Sql Server 2008的简单数据库应用
- 【网络】OSPF协议
- 持续集成引擎 Hudson 和 Jenkins 的恩恩怨怨
- 移动VR游戏被资本看好,Multiverse获数千万美元A轮融资
- TCP协议中URG&PSH