??????? Java字符串类(java.lang.String)是Java中使用最多的类,也是最为特殊的一个类,很多时候,我们对它既熟悉又陌生。
一.从根本上认识java.lang.String类和String池
??????? 首先,建议先看看String类的源码实现,这是从本质上认识String类的根本出发点。从中可以看到:
1.String类是final的,不可被继承。public final class String。
2.String类是的本质是字符数组char[], 并且其值不可改变。private final char value[];
然后打开String类的API文档,可以发现:
3.String类对象有个特殊的创建的方式,就是直接指定比如String x = "abc","abc"就表示一个字符串对象。而x是"abc"对象的地址,也叫做"abc"对象的引用。
4.String对象可以通过“+”串联。串联后会生成新的字符串。也可以通过concat()来串联,这个后面会讲述。
5.Java运行时会维护一个String Pool(String池),JavaDoc翻译很模糊“字符串缓冲区”。String池用来存放运行时中产生的各种字符串,并且池中的字符串的内容不重复。而一般对象不存在这个缓冲池,并且创建的对象仅仅存在于方法的堆栈区。下面是个系统内存示意图:
实例1:
package com.bijian.study.string;
public class StringTest {
public static void main(String[] args) {
/*
* "sss111"是编译期常量,编译时已经能确定它的值,在编译 好的class文件中它已经在String Pool中了,此语句会在
* String Pool中查找等于"sss111"的字符串(用equals(Object)方法确定),
* 如果存在就把引用返回,付值给s1.不存在就会创建一个"sss111"放在 String Pool中,然后把引用返回,付值给s1.
*/
String s1 = "sss111";
// 此语句同上
String s2 = "sss111";
/*
* 由于String Pool只会维护一个值相同的String对象 上面2句得到的引用是String Pool中同一个对象,所以 他们引用相等
*/
System.out.println(s1 == s2); // 结果为true
}
}
实例2:
package com.bijian.study.string;
public class StringTest {
public static void main(String[] args) {
/*
* 在java中,使用new关键字会创建一个新对象,在本例中,不管在 String Pool中是否已经有值相同的对象,都会创建了一个新的
* String对象存储在heap中,然后把引用返回赋给s1. 本例中使用了String的public String(String
* original)构造函数.
*/
String s1 = new String("sss111");
/*
* 此句会按照例1中所述在String Pool中查找
*/
String s2 = "sss111";
/*
* 由于s1是new出的新对象,存储在heap中,s2指向的对象 存储在String Pool中,他们肯定不是同一个对象,只是
* 存储的字符串值相同,所以返回false.
*/
System.out.println(s1 == s2); // 结果为false
}
}
实例3:
package com.bijian.study.string;
public class StringTest {
public static void main(String[] args) {
String s1 = new String("sss111");
/*
* 当调用intern方法时,如果String Pool中已经包含一个等于此String对象 的字符串(用
* equals(Object)方法确定),则返回池中的字符串.否则,将此 String对象添加到池中,并返回此String对象在String
* Pool中的引用.
*/
s1 = s1.intern();
String s2 = "sss111";
/*
* 由于执行了s1 = s1.intern(),会使s1指向String Pool中值为"sss111"
* 的字符串对象,s2也指向了同样的对象,所以结果为true
*/
System.out.println(s1 == s2);
}
}
实例4:
package com.bijian.study.string;
public class StringTest {
public static void main(String[] args) {
String s1 =new String("111");
String s2 ="sss111";
/*
* 由于进行连接的2个字符串都是常量,编译期就能确定连接后的值了,
* 编译器会进行优化直接把他们表示成"sss111"存储到String Pool中,
* 由于上边的s2="sss111"已经在String Pool中加入了"sss111",
* 此句会把s3指向和s2相同的对象,所以他们引用相同.此时仍然会创建出
* "sss"和"111"两个常量,存储到String Pool中.
*/
String s3 ="sss"+"111";
/*
* 由于s1是个变量,在编译期不能确定它的值是多少,所以
* 会在执行的时候创建一个新的String对象存储到heap中,
* 然后赋值给s4.
*/
String s4 ="sss"+ s1;
System.out.println(s2 == s3); //true
System.out.println(s2 == s4); //false
System.out.println(s2 == s4.intern()); //true
}
}
实例5:
??????? StringTest.java
package com.bijian.study.string;
public class StringTest {
public static void main(String[] args) {
String hello = "Hello", lo = "lo";
System.out.println((hello == "Hello") + ""); //true
System.out.println((Other.hello == hello) + ""); //true
System.out.println((com.bijian.study.other.Other.hello == hello) + ""); //true
System.out.println((hello == ("Hel" + "lo")) + ""); //true
System.out.println((hello == ("Hel" + lo)) + ""); //false
System.out.println(hello == ("Hel" + lo).intern()); //true
}
}
class Other {
static String hello = "Hello";
}
??????? Other.java
package com.bijian.study.other;
public class Other {
public static String hello ="Hello";
}
?
四.认识trim()、concat()
实例:
package com.bijian.study.string;
public class StringTest {
public static void main(String[] args) {
// 在池中和堆中分别创建String对象"abc",s1指向堆中对象
String s1 = new String("abc");
// s2直接指向池中对象"abc"
String s2 = "abc";
// 在堆中新创建"abc"对象,s3指向该对象
String s3 = new String("abc");
// 在池中创建对象"ab" 和 "c",并且s4指向池中对象"abc"
String s4 = "ab" + "c";
// c指向池中对象"c"
String c = "c";
// 在堆中创建新的对象"abc",并且s5指向该对象
String s5 = "ab" + c;
String s6 = "ab".concat("c");
String s7 = "ab".concat(c);
System.out.println("------------实串-----------");
System.out.println(s1 == s2); // false
System.out.println(s1 == s3); // false
System.out.println(s2 == s3); // false
System.out.println(s2 == s4); // true
System.out.println(s2 == s5); // false
System.out.println(s2 == s6); // false
System.out.println(s2 == s7); // false
}
}
?
五.认识空格、空串、null
实例:
package com.bijian.study.string;
public class StringTest {
public static void main(String[] args) {
String b1 = new String("");
String b2 = "";
String b3 = new String("");
String b4 = "".intern();
String b5 = "" + "";
String b6 = "".concat("");
String b7 = " ".trim();
String b8 = " ";
String b9 = " ".trim();
System.out.println("------------空串-----------");
System.out.println(b1 == b2); //false
System.out.println(b1 == b3); //false
System.out.println(b2 == b3); //false
System.out.println(b2 == b4); //true
System.out.println(b2 == b5); //true
System.out.println(b2 == b6); //true
System.out.println(b2 == b7); //false
System.out.println("-----a----");
System.out.println(b2.equals(b7)); //true
System.out.println(b7 == b8); //false
System.out.println(b7 == b9); //false
System.out.println(b7.equals(b9)); //true
System.out.println(b9 == null);//false
System.out.println("b8.trim():");
for (byte b : b8.getBytes()) {
System.out.print(">>>" + (int) b + " "); //>>>32 >>>32
}
System.out.println("\nb8.trim():");
for (byte b : b8.trim().getBytes()) {
System.out.print(">>>" + (int) b + " "); //
}
System.out.println("\nb9.trim():");
for (byte b : b9.trim().getBytes()) {
System.out.print(">>>" + (int) b + " "); //
}
}
}
运行结果:
------------空串----------- false false false true true true false -----a---- true false false true false b8.trim(): >>>32 >>>32 b8.trim(): b9.trim():
?
六.String的常见用法
1.字符串重编码
??????? 这个问题说来比较简单,转码就一行搞定,不信你看看,但究竟为什么要转码,是个很深奥的问题,看实例:?
package com.bijian.study.string;
import java.io.UnsupportedEncodingException;
public class StringTest {
public static void main(String[] args) throws UnsupportedEncodingException {
System.out.println("转码前,输出Java系统属性如下:");
System.out.println("user.country:" + System.getProperty("user.country"));
System.out.println("user.language:" + System.getProperty("user.language"));
System.out.println("sun.jnu.encoding:" + System.getProperty("sun.jnu.encoding"));
System.out.println("file.encoding:" + System.getProperty("file.encoding"));
System.out.println("---------------");
String s = "字符串测试";
String s1 = new String(s.getBytes(), "UTF-8");
String s2 = new String(s.getBytes("UTF-8"), "UTF-8");
String s3 = new String(s.getBytes("UTF-8"));
String s4 = new String(s.getBytes("UTF-8"), "GBK");
String s5 = new String(s.getBytes("GBK"));
String s6 = new String(s.getBytes("GBK"), "GBK");
System.out.println(s1);
System.out.println(s2);
System.out.println(s3);
System.out.println(s4);
System.out.println(s5);
System.out.println(s6);
}
}
运行结果:
转码前,输出Java系统属性如下: user.country:CN user.language:zh sun.jnu.encoding:GBK file.encoding:UTF-8 --------------- 字符串测试 字符串测试 字符串测试 瀛楃涓叉祴璇? ?????? 字符串测试
小结:
a.转一个码,又用该码来构建一个字符串,是绝对不会出现乱码的,----你相当于没转。
b.转码与否,与字符串本身编码有关,字符串本身的编码与谁有关?----文件编码,或者你的IDE设置的编码有关。
在此,我用的IDEA开发工具,默认是UTF-8编码,但操作系统使用的是GBK,但没有问题,我只要按照UTF-8来读取我的字符串就不会有乱码。但是文件已经是UTF-8了,你非要转为GBK,不乱才怪!那有什么办法呢?在Windows下,用记事本或者Editplus打开后另存为(并修改编码方式即可)。
?
2.字符比较
不就是个匹配关系吗?String类的API有一些可以做比较,如果不行,可以寻求正则表达式来解决。
?
3.获取某个字符
获取一个字符序列toCharArray() ,然后就随便玩去吧,中文就乱了。
?
4.字符串的截取
substring()
?
5.字符串的替换与查找
?
6.开始结束判断
startsWith()/endWith()
?
7.字符串的排序比较
compareTo(String anotherString):按字典顺序比较两个字符串。
compareToIgnoreCase(String str):不考虑大小写,按字典顺序比较两个字符串。
?
8.字符串的equals()和hashCode()
已经实现了好了,直接调用,不用重写
?
9.字符串的类型转化
太多了,String.valueOf()系列很多。
类似的Long.parseLong(String s)
?
10.字符串的复制
copyValueOf()
?
11.大小写转换
toLowerCase()
toUpperCase()
?
12.正则匹配
?
文章来源:http://lavasoft.blog.51cto.com/62575/80034/