一、 String
• 字符串指的是字符的序列,有两种类型的字符串:一种是创建以后不需要改变的,称为字符串常量,在 Java 中,String 类用于存储和处理字符串常量;另外一种字符串是创建以后,需要对其进行改变的,称为字符串变量,在 Java中,StringBuffer 类用于存储和操作字符串变量。
• 注意:字符串不是 char[]数组
• 字符串內部各个字符的排列位置是连续的,位置编号由开始到结束是从 0 到 n。
创建 String 对象示例:
String s1 = “Egg”;
或者
String s2 = new String("Egg");
区别:编译器遇到像“Egg”这样的“字符串常量”时,它会自动创建一个含有“Egg”字符串的对象,然后把该对象的引用传给 s1。而处理 s2 时,编译器一遇到构造函数的参数“Egg”时,会首先创建一个含有“Egg”字符串的对象(匿名的)。然后,根据构造函数“new String(”Egg“)”的要求,再创建出另一个含有“Egg”字符串的对象(也是匿名的),然后把该对象的引用传给 s2。这样一来,就多创建了一个匿名的 String 对象。
(1)字符串的连接
public class Test {
public static void main(String[] args) {
String string = "cao";
String string1 = string.concat("haodong");
System.out.println(string);
System.out.println(string1);
}
}
cao
caohaodong
(2)求字符串的长度
public class Test {
public static void main(String[] args) {
String string = "cao";
//求字符串长度
int len=string.length();
System.out.println(len);
}
}
3
(3)求字符串中某一位置的字符
public class Test {
public static void main(String[] args) {
String string = "cao";
//求字符串中某一位置的字符
char charOfString=string.charAt(1);
System.out.println(charOfString);
}
}
a
(4)字符串的比较
compareTo
public class Test {
public static void main(String[] args) {
//符串的比较
int compare="abc".compareTo("abc");
int compare1="abc".compareTo("acc");
int compare2="abc".compareTo("aac");
System.out.println(compare+"\t"+compare1+"\t"+compare2);
}
}
0 -1 1
euqals
public class Test {
public static void main(String[] args) {
//equals方法
boolean b="abc".equals("abc");
boolean b1="abc".equals("aaa");
System.out.println(b+"\t"+b1);
}
}
true false
equalsIgnoreCase
public class Test {
public static void main(String[] args) {
//equalsIgnoreCase方法
boolean b2="HELLO".equalsIgnoreCase("hello");
System.out.println(b2);
}
}
true
(5)从字符串中提取子串
public class Test {
public static void main(String[] args) {
//从字符串中提取子串
String afterSub="hello".substring(3);
System.out.println(afterSub);
afterSub="hello".substring(2,4);
System.out.println(afterSub);
}
}
lo
ll
(6)判断字符串的前缀和后缀
public class Test {
public static void main(String[] args) {
//判断字符串的前缀和后缀
boolean b3="hello".startsWith("he");
System.out.println(b3);
b3="hello".startsWith("ll",2);
System.out.println(b3);
b3="hello".endsWith("lo");
System.out.println(b3);
}
}
true
true
true
(7)字符串中单个字符的查找
public class Test {
public static void main(String[] args) {
//字符串中单个字符的查找
int index="hello".indexOf("l");
System.out.println(index);
index="hello".indexOf("el");
System.out.println(index);
index="hello".indexOf("el",2);
System.out.println(index);
//lastIndexOf
index="hello".lastIndexOf("l");
System.out.println(index);
index="hello".lastIndexOf("l",2);
System.out.println(index);
}
}
2
1
-1
3
2
(8)字符串中子串的查找
public class Test {
//字符串中子串的查找
int exist="cao hao dong 666...".indexOf("chen");
System.out.println(exist);
exist="cao hao dong 666...".lastIndexOf("tang");
System.out.println(exist);
}
}
-1
-1
(9)字符串中字符大小写的转换
public class Test {
public static void main(String[] args) {
//字符串中字符大小写的转换
String upper="hello".toUpperCase();
System.out.println(upper);
String low="HELLO".toLowerCase();
System.out.println(low);
}
}
HELLO
hello
(10)字符串中多余空格的去除
package com.cao.demo.lesson00;
public class Test {
public static void main(String[] args) {
//字符串中多余空格的去除
//字符串中间的空格并不去掉
String afterTrim=" hello ".trim();
System.out.println(afterTrim);
}
}
hello
(11)字符串中字符的替换
package com.cao.demo.lesson00;
public class Test {
public static void main(String[] args) {
//字符串中字符的替换
//public String replace(char oldChar,char newChar)
String afterReplace="hello".replace("l","6666");
System.out.println(afterReplace);
//public String replaceFirst(String regex, String replacement)
afterReplace="hello hello".replaceFirst("ll","6666");
System.out.println(afterReplace);
//public String replaceAll(String regex, String replacement)
afterReplace="hello hello".replaceAll("ll","6666");
System.out.println(afterReplace);
}
}
he66666666o
he6666o hello
he6666o he6666o
• 因为 String 对象的內容是不可以被更改的,所以以上的方法都不会改变 String 对象的內容。 当返回值类型为 String 时,实际上是会产生一个新的 String 对象。
二、StringBuffer 类
StringBuffer 类对象表示的是字符串变量,每一个 StringBuffer 类对象都是可以扩充和修改的字符串变量
(1)StringBuffer 类对象的扩充
public class Test {
public static void main(String[] args) {
StringBuffer stringBuffer=new StringBuffer("cao hao dong "); // 不可以使用字面量的方式来创建
//public StringBuffer append(Object obj)
stringBuffer.append("chen xiao wei");
System.out.println(stringBuffer);
//public StringBuffer insert()
stringBuffer.insert(3,"·");
System.out.println(stringBuffer);
cao hao dong chen xiao wei
cao· hao dong chen xiao wei
(2)StringBuffer 类对象的长度不容量
package com.cao.demo.lesson00;
public class Test {
public static void main(String[] args) {
//StringBuffer 类对象的长度和容量
//public int length()
int sbLength=new StringBuffer("hello").length();
System.out.println(sbLength);
//public int capacity()
int sbCapacity=new StringBuffer("").capacity();
System.out.println(sbCapacity);
sbCapacity=new StringBuffer("1").capacity();
System.out.println(sbCapacity);
sbCapacity=new StringBuffer("11").capacity();
System.out.println(sbCapacity);
}
}
5
16
17
18
(3)StringBuffer 类对象的修改
package com.cao.demo.lesson00;
public class Test {
public static void main(String[] args) {
//StringBuffer 类对象的修改
StringBuffer stringBuffer1=new StringBuffer("hello");
stringBuffer1.setCharAt(0,'H');
System.out.println(stringBuffer1);
}
}
Hello
因为 StringBuffer 对象的內容可以被更改,所以以上的方法会改变 StringBuffer 对象的內容。当返回值类型为String 时,实际上是会传回原来的 StringBuffer 对象。
三、String 与 StringBuffer 的区别
• String 不与StringBuffer。它们之间所有的差別是在于
– String 对象的內容是不可以被更改;而 StringBuffer 的对象內容则是可以被更改的。
– String 类型的字串其长度是固定的;而 StringBuffer 类型的字串其长度是变动、并非固定的。
• 如果只需要读取字串其中的內容时,String 类型则已足够
四、注意点
public class Test1 {
public static void main(String[] args) {
StringBuffer stringBuffer = new StringBuffer("cao");
StringBuffer stringBuffer1 = new StringBuffer("cao");
System.out.println(stringBuffer==stringBuffer1);
System.out.println(stringBuffer.toString().equals(stringBuffer1.toString()));
System.out.println(stringBuffer.toString().compareTo(stringBuffer1.toString()));
}
}
false
true
0
equals()、compareTo()方法均是 String 的方法,而不是 StringBuffer 的方法,如果 StringBuffer 需要使用必须先使用 toString()
==
比较的是两个字符串的地址是否相同(同一个地址)
equals()
方法比较的是两个字符串的内容是否相同(若两个字符串引用同一个地址,使用equals()比较也返回true)
以下引用自https://blog.csdn.net/DD04567/article/details/107143006
public class Test2 {
public static void main(String[] args) {
String s1 = "xyz";
String s2 = "xyz";
String s3 = new String("xyz");
String s4 = new String("xyz");
System.out.println(s1==s2);
System.out.println(s1==s3);
System.out.println(s3==s4);
System.out.println("----------");
System.out.println(s1.equals(s2));
System.out.println(s1.equals(s3));
System.out.println(s3.equals(s4));
System.out.println("----------");
}
}
true
false
false
----------
true
true
true
----------
从内存结构看出存在一块区域 叫做常量池
代码中, 我们 String s1 = “xyz”; String s2 = “xyz”;
这样声明的字符串对象,其值就是存放在常量池中的
当我们 String s1 = "xyz"
这样声明s1的时候,xyz就会被存放到常量池中,当我们再次声明s2的时候,java就会优先在常量池中查找是否存在xyz,如果存在,就会让s2指向这个值而不会再去重新创建。所以我们使用==去比较s1,s2的时候,由于地址相同,返回true。
众所周知,new出来的对象都是存放在堆中的
每一个new出来的对象都会在堆中单独分配一块内存,并在栈中存放该对象的引用变量(见内存结构图,图中s1,s2存放的就是指向对象的地址),所以代码中s1==s3
和 s3==s4
返回的都是false,因为地址不相同。
equals()方法比较的是字符串的内容是否相同
代码中字符串内容都是xyz,所以自然返回的就是true。
上述内容都是针对String字符串而言的!!!!
对于基本类型而言:
==
就是比较的值是否相同,而equals()
不能用来比较基本数据类型,否则编译器直接报错。
对于引用数据类型 (重写了equals()方法的除外,例如String类型)
==
和equals()
比较的都是地址是否相同。
public class Text {
public static void main(String[] args) {
Animal animal1 = new Animal("dog");
Animal animal2 = new Animal("dog");
System.out.println(animal1.equals(animal2));
System.out.println(animal1==animal2);
}
}
class Animal{
public String name;
public Animal() { }
public Animal(String name) {
this.name = name;
}
}
false
false
由代码可以看出,不管是使用==
还是equals()
方法,==
不同是因为地址不同,但是equals()
方法比较的是内容,为什么还不相同呢?原因就在于Animal类没有重写equals()
方法,而且在堆中的地址又是不同的,所以,equals()
和==
输出的都是false。
而!!!String恰恰重写了equals()
方法!!!,由于java重写了Stirng类的equals()
方法,所以我们才说==
比较的是地址,而equals()
比较的是内容。