String
说明:String的本质是字符序列,它是通过字符数组实现的!
从表面上看,字符串就是双引号之间的数据,例如"百度"、 "http://www.baidu.com"等。在Java中,可以使用下面的方法定义字符串:
String url = "http://www.baidu.com";
String webName = "百度";字符串可以通过“+”连接,基本数据类型与字符串进行“+”操作一般也会自动转换为字符串,
示例:
String的各种构造函数的使用方法!
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();
}
}
}运行结果
String 将各种对象转换成String的API
运行结果:
String 中index相关的API
方法:
注意一点个:JAVA String方法中public int indexOf(int ch)问题,下面的代码也做了示例对比
示例:
运行结果:
可以参考这个百度百科的回答
String “比较”操作的API
运行结果:
String “修改(追加/替换/截取/分割)”操作的API
运行结果:
String 操作Unicode的API
运行结果:
String 剩余的API
运行结果
Last updated
Was this helpful?