1.认识并创建字符串
在c语言里并没有字符串这种数据类型,而在Java中是有这种类型的。那什么叫做字符串呢?
就是用双引号引起来的若干个字符常量(也可以为一个),而字符是由单引号引起的单个字符常量。注意:在Java中没有所谓的/0作为字符串结束的标致。
常见的构造 String 的方式:
// 方式一 String str = "Hello Bit"; // 方式二 方式一二构造String的方式本质相同 String str2 = new String("Hello Bit"); // 方式三 方式三就是把数组变为了字符串 char[] array = {'a', 'b', 'c'}; String str3 = new String(array);
常见的坑:
public static void fun(String str,char[] ch) { str="hello"; ch[0]='A'; } public static void main(String[] args) { String str=new String("abcd"); char[] ch={'c','s','d','n'}; fun(str,ch); System.out.println(str); System.out.println(Arrays.toString(ch)); } //:运行结果 abcd [A, s, d, n]
我们可以发现,在fun函数内改变str引用时,并没有对main函数中的str造成影响,每一个常量都会开辟一块空间,fun中的str只是将引用指向了新的常量,所以并不会对main中的str做出改变,而数组则通过[]符合角标访问,对数组内数据发生了实际改变。
【注意】
1.String是引用类型,内部并不存储字符串本身,在String类的实现源码中,String类实例变量如下:
public static void main(String[] args) { // s1和s2引用的是不同对象 s1和s3引用的是同一对象 String s1 = new String("hello"); String s2 = new String("world"); String s3 = s1; System.out.println(s1.length()); // 获取字符串长度---输出5 System.out.println(s1.isEmpty()); // 如果字符串长度为0,返回true,否则返回false }
2.在Java中“”引起来的也是String类型对象。
// 打印"hello"字符串(String对象)的长度 System.out.println("hello".length());
2. 字符串对象的比较
字符串的比较是常见操作之一,比如:字符串排序。Java中总共提供了4中方式:
1.==比较是否引用同一个对象
注意:对于内置类型,== 比较的是变量中的值;对于引用类型 == 比较的是引用中的地址。
public static void main(String[] args) { int a = 10; int b = 20; int c = 10; // 对于基本类型变量,==比较两个变量中存储的值是否相同 System.out.println(a == b); // false System.out.println(a == c); // true // 对于引用类型变量,==比较两个引用变量引用的是否为同一个对象 String s1 = new String("hello"); String s2 = new String("hello"); String s3 = new String("world"); String s4 = s1; System.out.println(s1 == s2); // false System.out.println(s2 == s3); // false System.out.println(s1 == s4); // true }
2.boolean equals(Object anObject) 方法:按照字典序比较
字典序:字符大小的顺序
String类重写了父类Object中equals方法,Object中equals默认按照==比较,String重写equals方法后,按照
如下规则进行比较,比如:s1.equals(s2)
public boolean equals(Object anObject) { // 1. 先检测this 和 anObject 是否为同一个对象比较,如果是返回true if (this == anObject) { return true; } // 2. 检测anObject是否为String类型的对象,如果是继续比较,否则返回false if (anObject instanceof String) { // 将anObject向下转型为String类型对象 String anotherString = (String)anObject; int n = value.length; // 3. this和anObject两个字符串的长度是否相同,是继续比较,否则返回false if (n == anotherString.value.length) { char v1[] = value; char v2[] = anotherString.value; int i = 0; // 4. 按照字典序,从前往后逐个字符进行比较 while (n-- != 0) { if (v1[i] != v2[i]) return false; i++; } return true; } } return false; }
public static void main(String[] args) { String s1 = new String("hello"); String s2 = new String("hello"); String s3 = new String("Hello"); // s1、s2、s3引用的是三个不同对象,因此==比较结果全部为false System.out.println(s1 == s2); // false System.out.println(s1 == s3); // false // equals比较:String对象中的逐个字符 // 虽然s1与s2引用的不是同一个对象,但是两个对象中放置的内容相同,因此输出true // s1与s3引用的不是同一个对象,而且两个对象中内容也不同,因此输出false System.out.println(s1.equals(s2)); // true System.out.println(s1.equals(s3)); // false }
3.int compareTo(String s) 方法: 按照字典序进行比较
与equals不同的是,equals返回的是boolean类型,而compareTo返回的是int类型。具体比较方式:
先按照字典次序大小比较,如果出现不等的字符,直接返回这两个字符的大小差值
如果前k个字符相等(k为两个字符长度最小值),返回值两个字符串长度差值
public static void main(String[] args) { String s1 = new String("abc"); String s2 = new String("ac"); String s3 = new String("abc"); String s4 = new String("abcdef"); System.out.println(s1.compareTo(s2)); // 不同输出字符差值-1 System.out.println(s1.compareTo(s3)); // 相同输出 0 System.out.println(s1.compareTo(s4)); // 前k个字符完全相同,输出长度差值 -3 }
4.int compareToIgnoreCase(String str) 方法:与compareTo方式相同,但是忽略大小写比较
public static void main(String[] args) { String s1 = new String("abc"); String s2 = new String("ac"); String s3 = new String("ABc"); String s4 = new String("abcdef"); System.out.println(s1.compareToIgnoreCase(s2)); // 不同输出字符差值-1 System.out.println(s1.compareToIgnoreCase(s3)); // 相同输出 0 System.out.println(s1.compareToIgnoreCase(s4)); // 前k个字符完全相同,输出长度差值 -3 }
3.字符串常量池
3.1 创建对象的思考
下面两种创建String对象的方式相同吗?
public static void main(String[] args) { String s1 = "hello"; String s2 = "hello"; String s3 = new String("hello"); String s4 = new String("hello"); System.out.println(s1 == s2); // true System.out.println(s1 == s3); // false System.out.println(s3 == s4); // false }
上述程序创建方式类似,为什么s1和s2引用的是同一个对象,而s3和s4不是呢?
在Java程序中,类似于:1, 2, 3,3.14,“hello”等字面类型的常量经常频繁使用,为了使程序的运行速度更快、更节省内存,Java为8种基本数据类型和String类都提供了常量池。
“池” 是编程中的一种常见的, 重要的提升效率的方式, 我们会在未来的学习中遇到各种 “内存池”, “线程池”, “数据库连接池” …
比如:家里给大家打生活费的方式
家里经济拮据,每月定时打生活费,有时可能会晚,最差情况下可能需要向家里张口要,速度慢
家里有矿,一次性打一年的生活费放到银行卡中,自己随用随取,速度非常快 方式2,就是池化技术的一种示例,钱放在卡上,随用随取,效率非常高。常见的池化技术比如:数据库连接 池、线程池等。
为了节省存储空间以及程序的运行效率,Java中引入了:
Class文件常量池:每个.Java源文件编译后生成.Class文件中会保存当前类中的字面常量以及符号信息
运行时常量池:在.Class文件被加载时,.Class文件中的常量池被加载到内存中称为运行时常量池,运行时常量池每个类都有一份
字符串常量池
3.2 字符串常量池(StringTable)
再谈String对象创建:
1.直接使用字符串常量进行赋值
public static void main(String[] args) { String s1 = "hello"; String s2 = "hello"; System.out.println(s1 == s2); // true }
2.通过new创建String类对象
结论:只要是new的对象,都是唯一的。
通过上面例子可以看出:使用常量串创建String类型对象的效率更高,而且更节省空间。用户也可以将创建的字符串对象通过 intern 方式添加进字符串常量池中。
3.intern方法
该方法的作用是手动将创建的String对象添加到常量池中。
public static void main(String[] args) { char[] ch = new char[]{'a', 'b', 'c'}; String s1 = new String(ch); // s1对象并不在常量池中 //s1.intern(); // s1.intern();调用之后,会将s1对象的引用放入到常量池中 String s2 = "abc"; // "abc" 在常量池中存在了,s2创建时直接用常量池中"abc"的引用 System.out.println(s1 == s2); } // 输出false // 将上述方法打开之后,就会输出true
面试题:请解释String类中两种对象实例化的区别 JDK1.8中
String str = “hello”
只会开辟一块堆内存空间,保存在字符串常量池中,然后str共享常量池中的String对象
String str = new String(“hello”)
会开辟两块堆内存空间,字符串"hello"保存在字符串常量池中,然后用常量池中的String对象给新开辟的String对象赋值。
String str = new String(new char[]{‘h’, ‘e’, ‘l’, ‘l’, ‘o’})
现在堆上创建一个String对象,然后利用copyof将重新开辟数组空间,将参数字符串数组中内容拷贝到String对象中
4.字符串不可变
String类在设计时就是不可改变的,String类实现描述中已经说明了
感受下形如这样的代码:
String类中的字符实际保存在内部维护的value字符数组中,该图还可以看出:
value是由private修饰的,在类外拿不到这个变量,所以字符串不能修改
代码示例:
String str = "hello" ; str = str + " world" ; str += "!!!" ; System.out.println(str); // 执行结果 hello world!!!
形如 += 这样的操作, 表面上好像是修改了字符串, 其实不是,而是在拼接成新的字符串后,str引用指向了新的字符串。
如果真的想要改变字符串常量,只能通过反射的方式来进行,反射我们在后边将会学习。
5.转化
1.数值和字符串转化
public static void main(String[] args) { // 数字转字符串 String s1 = String.valueOf(1234); String s2 = String.valueOf(12.34); System.out.println(s1); System.out.println(s2); System.out.println("================================="); // 字符串转数字 // 注意:Integer、Double等是Java中的包装类型,这个后面会讲到 int data1 = Integer.parseInt("1234"); double data2 = Double.parseDouble("12.34"); System.out.println(data1); System.out.println(data2); }
2.大小写转换
public static void main(String[] args) { String s1 = "hello"; String s2 = "HELLO"; // 小写转大写 System.out.println(s1.toUpperCase()); // 大写转小写 System.out.println(s2.toLowerCase()); }
3.字符串转数组
public static void main(String[] args) { String s = "hello"; // 字符串转数组 char[] ch = s.toCharArray(); for (int i = 0; i < ch.length; i++) { System.out.print(ch[i]); } System.out.println(); // 数组转字符串 String s2 = new String(ch); System.out.println(s2); }
4.格式化
public static void main(String[] args) { String s = String.format("%d-%d-%d", 2019, 9,14); System.out.println(s); }
6.字符串常见操作
6.1 字符串比较
上面使用过String类提供的equals()方法,该方法本身是可以进行区分大小写的相等判断。除了这个方法之外,String类还提供有如下的比较操作:
代码示例: 不区分大小写比较
String str1 = "hello" ; String str2 = "Hello" ; System.out.println(str1.equals(str2)); // false System.out.println(str1.equalsIgnoreCase(str2)); // true
在String类中compareTo()方法是一个非常重要的方法,该方法返回一个整型,该数据会根据大小关系返回三类内容:
- 相等:返回0.
- 小于:返回内容小于0.
- 大于:返回内容大于0。
范例:观察compareTo()比较
System.out.println("A".compareTo("a")); // -32 System.out.println("a".compareTo("A")); // 32 System.out.println("A".compareTo("A")); // 0 System.out.println("AB".compareTo("AC")); // -1 System.out.println("刘".compareTo("杨"));
compareTo()是一个可以区分大小关系的方法,是String方法里是一个非常重要的方法。
字符串的比较大小规则, 总结成三个字 “字典序” 相当于判定两个字符串在一本词典的前面还是后面. 先比较第一个字符的大小(根据
unicode 的值来判定), 如果不分胜负, 就依次比较后面的内容。
6.2 字符串查找
从一个完整的字符串之中可以判断指定内容是否存在,对于查找方法有如下定义:
代码示例: 字符串查找,最好用最方便的就是contains()
String str = "helloworld" ; System.out.println(str.contains("world")); // true
我们可以发现contains方法内的参数类型为CharSequence,是因为String方法已经连接了CharSequence接口,此处是发生了向上转型。
代码示例: 使用indexOf()方法进行位置查找
String str = "helloworld" ; System.out.println(str.indexOf("world")); // 5,w开始的索引 System.out.println(str.indexOf("bit")); // -1,没有查到 if (str.indexOf("hello") != -1) { System.out.println("可以查到指定字符串!"); }
现在基本都是用contains()方法完成。
使用indexOf()需要注意的是,如果内容重复,它只能返回查找的第一个位置。
代码示例: 使用indexOf()的注意点
String str = "helloworld" ; System.out.println(str.indexOf("l")); // 2 System.out.println(str.indexOf("l",5)); // 8 System.out.println(str.lastIndexOf("l")); // 8
在进行查找的时候往往会判断开头或结尾。
代码示例: 判断开头或结尾
String str = "**@@helloworld!!" ; System.out.println(str.startsWith("**")); // true System.out.println(str.startsWith("@@",2)); // ture System.out.println(str.endsWith("!!")); // true
6.3 字符串替换
使用一个指定的新的字符串替换掉已有的字符串数据,可用的方法如下:
代码示例: 字符串的替换处理
String str = "helloworld" ; System.out.println(str.replaceFirst("l", "_")); System.out.println(str.replaceAll("l", "_")); //运行结果 he_loworld he__owor_d
注意事项: 由于字符串是不可变对象, 替换不修改当前字符串, 而是产生一个新的字符串。
6.4 字符串拆分
可以将一个完整的字符串按照指定的分隔符划分为若干个子字符串。
可用方法如下:
代码示例: 实现字符串的拆分处理
String str = "hello world hello csdn" ; String[] result = str.split(" ") ; // 按照空格拆分 for(String s: result) { System.out.println(s); } // hello world hello csdn
代码示例: 字符串的部分拆分
String str = "hello world hello bit" ; String[] result = str.split(" ",2) ; for(String s: result) { System.out.println(s); } // hello world hello csdn
拆分是特别常用的操作. 一定要重点掌握. 另外有些特殊字符作为分割符可能无法正确切分, 需要加上转义.
代码示例: 拆分IP地址
String str = "192.168.1.1" ; String[] result = str.split("\\.") ; for(String s: result) { System.out.println(s); } // 192 168 1 1
注意事项:
- 字符"|“,”*“,”+“都得加上转义字符,前面加上”".
- 而如果是"“,那么就得写成”\".
- 如果一个字符串中有多个分隔符,可以用"|"作为连字符.
代码示例: 多次拆分
String str = "name=zhangsan&age=18" ; String[] result1 = str.split("&") ; for(String t1 : result1) { String[] result2=t1.split("="); for(String t2 : result2) { System.out.println(t2); } } // name zhangsan age 18
6.5 字符串截取
从一个完整的字符串之中截取出部分内容。可用方法如下:
代码示例: 观察字符串截取
String str = "helloworld" ; System.out.println(str.substring(5)); System.out.println(str.substring(0, 5)); // world hello
注意事项:
索引从0开始
注意前闭后开区间的写法, substring(0, 5) 表示包含 0 号下标的字符, 不包含 5 号下标
6.6 其他操作方法
代码示例: 观察trim()方法的使用
String str = " hello world " ; System.out.println("["+str+"]"); System.out.println("["+str.trim()+"]"); // [ hello world ] [hello world]
trim 会去掉字符串开头和结尾的空白字符(空格, 换行, 制表符等).
代码示例: 大小写转换
String str = " heLLo%$$%@#$%world 哈哈哈 " ; System.out.println(str.toUpperCase()); System.out.println(str.toLowerCase()); // HELLO%$$%@#$%WORLD 哈哈哈 hello%$$%@#$%world 哈哈哈
这两个函数只转换字母。
代码示例: 字符串length()
String str = " hello%$$%@#$%world 哈哈哈 " ; System.out.println(str.length()); // 24
**注意:**数组长度使用数组名称.length属性,而String中使用的是length()方法
代码示例: 观察isEmpty()方法
System.out.println("hello".isEmpty()); System.out.println("".isEmpty()); System.out.println(new String().isEmpty()); // false true true
String类并没有提供首字母大写操作,需要自己实现
代码示例: 首字母大写
public static void main(String[] args) { System.out.println(fistUpper("yuisama")); System.out.println(fistUpper("")); System.out.println(fistUpper("a")); } public static String fistUpper(String str) { if ("".equals(str)||str==null) { return str ; } if (str.length()>1) { return str.substring(0, 1).toUpperCase()+str.substring(1) ; } return str.toUpperCase() ; } // Yuisama A
7. StringBuffer 和 StringBuilder
7.1初识
任何的字符串常量都是String对象,而且String的常量一旦声明不可改变,如果改变对象内容,改变的是其引用的指向而已。
通常来讲String的操作比较简单,但是由于String的不可更改特性,为了方便字符串的修改,提供StringBuffer和StringBuilder类(这两个类不能直接拿常量来赋值,需要先实例化对象)。
以下先由StringBuilder来举例:
两种赋初值方式:
public static void main(String[] args) { StringBuilder s1=new StringBuilder("abc");//一是构造方法赋初值 StringBuilder s2=new StringBuilder(); s2.append("abc");//二是调用append方法 System.out.println(s1); System.out.println(s2.toString());//调不调toString方法都可以打印 } // abc abc
拼接字符串:
public static void main(String[] args) { StringBuilder s=new StringBuilder(); s.append("abc"); s.append("123"); //也可以连用s.append("abc").append("123"); System.out.println(s); } // abc123
若该代码的实现要是通过String类来实现只能通过“+”,而且会产生多个对象浪费空间,最后只是把s的指向更改了而已;而通过StringBuilder类可以直接在原字符串上进行更改。
由以下代码可以更好体现:
public static void main(String[] args) { String str="abcd"; StringBuilder s=new StringBuilder(); s.append(str); for (int i = 0; i < 10; i++) { s.append(i); } str=s.toString(); System.out.println(str); } //abcd0123456789
String和StringBuilder最大的区别在于:String的内容无法修改,而StringBuilder的内容可以修改。频繁修改字符串的
情况考虑使用StringBuilder。
7.2 区别与转换
StringBuffer 和 StringBuilder 的区别:
我们分别点进StringBuffer 和 StringBuilder 的源码
StringBuffer :
StringBuilder:
可以观察到两个类里的重写方法只是一个词(synchronized)之差,synchronized就是为了维护线程安全,所以StringBuffer 和 StringBuilder 大部分功能是相同的(只是使用场合不同:单线程-StringBuilder 多线程-StringBuffer)
StringBuffer 或StringBuilder 与String类之间的转换:
注意:String和StringBuffer类不能直接转换。如果要想互相转换,可以采用如下原则:
String变为StringBuffer:利用StringBuffer的构造方法或append()方法
public StringBuilder func1() { String str="abc"; StringBuilder s=new StringBuilder(str); return s; } public StringBuilder func2() { String str="abc"; StringBuilder s=new StringBuilder(); s.append(str); return s; }
StringBuffer变为String:调用toString()方法 public String fun() { StringBuilder s=new StringBuilder("abc"); return s.toString(); }
7.3使用与总结
字符串反转:
public synchronized StringBuffer reverse()
代码示例: 字符串反转
StringBuffer sb = new StringBuffer("helloworld"); System.out.println(sb.reverse());
删除指定范围的数据:
public synchronized StringBuffer delete(int start, int end)
代码示例: 观察删除操作
StringBuffer sb = new StringBuffer("helloworld"); System.out.println(sb.delete(5, 10));
插入数据
public synchronized StringBuffer insert(int offset, 各种数据类型 b)
代码示例: 观察插入操作
StringBuffer sb = new StringBuffer("helloworld"); System.out.println(sb.delete(5, 10).insert(0, "你好")); //你好hello
面试题:
1.请解释String、StringBuffer、StringBuilder的区别?
- String的内容不可修改,StringBuffer与StringBuilder的内容可以修改.
- StringBuffer与StringBuilder大部分功能是相似的
- StringBuffer采用同步处理,属于线程安全操作;而StringBuilder未采用同步处理,属于线程不安全操作
2.以下总共创建了多少个String对象【前提不考虑常量池之前是否存在】
String str = new String("ab"); // 会创建2个对象 String str = new String("a") + new String("b"); // 会创建6个对象
再加上常量池中的a和b两个对象,一共六个。
8.小结
字符串操作是我们以后工作中非常常用的操作. 使用起来都非常简单方便, 一定要使用熟练.
注意的点:
- 字符串的比较, ==, equals, compareTo 之间的区别.
- 了解字符串常量池, 体会 “池” 的思想.
- 理解字符串不可变
- StringBuffer 和 StringBuilder 的功能.