String类的常用构造方法只有以下三种
public class Main {
public static void main(String[] args) {
String s1 = "hello";//使用常量串进行构造
String s2 = new String("hello");//创建String对象
char[] array = {'h','e','l','l','o'};//使用字符数组
String s3 = new String(array);
System.out.println(s1);
System.out.println(s2);
System.out.println(s3);
}
}
如要使用其他String方法,大家可以查看源码或者是jdk帮助手册
【注意】
public final class String
implements java.io.Serializable, Comparable<String>, CharSequence,
Constable, ConstantDesc {
private final byte[] value;//用于存字符串的数组,字符存储在这个数组中形成了字符串
private int hash; // 默认为0
public String(String original) {//对以上两个成员变量进行构造
this.value = original.value;
this.hash = original.hash;
}
}
下面我们用一段代码来说明String是引用类型
public class Main {
public static void main(String[] args) {
String s1 = "hello";
String s2 = new String("world");
char[] array = {'h','i'};//创建了3个不同的引用对象
String s3 = new String(array);
System.out.println(s1);
System.out.println(s2);
System.out.println(s3);
String s4 = s3;//s4和s3指向了同一个引用
System.out.println(s4);
}
}
字符串的引用方式其实和我们前面提到的数组很类似,因为底层就是数组实现的,我们下面通过画图说明

2. 在Java中,直接用双引号引起来的对象也是String类
System.out.println("hello");
public class Main {
public static void main(String[] args) {
String s1 = "hello";
String s2 = new String("world");
char[] array = {'h','i'};//创建了3个不同的引用对象
String s3 = new String(array);
System.out.println(s1);
System.out.println(s2);
System.out.println(s3);
String s4 = s3;//s4和s3指向了同一个引用
System.out.println(s4);
System.out.println(s3.length());
}
}

我们在这里需要注意的一点是,Java中的字符串不想c语言一样有以‘\0’这样的转义字符结尾的说法,有几个字母,字符串就是多长,在上面的运行结果我们可以看到,s3的长度为2
public class Main {
public static void main(String[] args) {
String s1 = "hello";
String s2 = new String("");//s2为空字符
char[] array = {'h','i'};//创建了3个不同的引用对象
String s3 = new String(array);
System.out.println(s1);
System.out.println(s2);
System.out.println(s3);
String s4 = s3;//s4和s3指向了同一个引用
System.out.println(s4);
System.out.println(s3.length());
System.out.println(s2.isEmpty());
}
}

从上述结果我们可以看出,isEmpty方法,如果字符串为空,返回ture,如果不为空,返回false
字符串的比较也是常见的操作之一,比如:字符串的排序,Java中总共提供了4种方式
public class Main {
public static void main(String[] args) {
int a = 1;
int b = 1;
int c = 2;
String s1 = "hello";
String s2 = "hi";
String s3 = "hello";
String s4 = s1;
System.out.println(a == b);//两个值相等
System.out.println(a == c);//两个值不相等
System.out.println(s1 == s2);//两个字符串不一样
System.out.println(s3 == s1);//虽然两个字符串相同,但是指向的引用不同
System.out.println(s1 == s4);//指向同一个引用
}
}

需要注意的是,由于String是引用类型,虽然s1和s3相同,但是它们的引用不同,所以返回false
那么我们如果想要比较两个字符串是否相同,我们有办法吗,当然有,我们下面来介绍它
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 class Main {
public static void main(String[] args) {
int a = 1;
int b = 1;
int c = 2;
String s1 = "hello";
String s2 = "hi";
String s3 = "hello";
String s4 = s1;
System.out.println(a == b);//两个值相等
System.out.println(a == c);//两个值不相等
System.out.println(s1 == s2);//两个字符串不一样
System.out.println(s3 == s1);//虽然两个字符串相同,但是指向的引用不同
System.out.println(s1 == s4);//指向同一个引用
System.out.println(s1.equals(s3));//两个字符串相同
System.out.println(s2.equals(s1));//两个字符串不同
}
}

上述代码我们可以看到,虽然s1和s3指向的是不同的引用,但是只要字符串相同,就返回true
3. int compareTo(String anotherString) 方法:按照字典序比较
与equals不同的是,equals返回的是boolean类型,而compareTo返回的是int类型。具体比较方式:
public class Main {
public static void main(String[] args) {
String s1 = "hello";
String s2 = "hi";
String s3 = "hello";
String s4 = "helloaaa";
System.out.println(s1 == s2);//两个字符串不一样
System.out.println(s3 == s1);//虽然两个字符串相同,但是指向的引用不同
System.out.println(s1 == s4);//指向同一个引用
System.out.println(s1.equals(s3));//两个字符串相同
System.out.println(s2.equals(s1));//两个字符串不同
System.out.println(s1.compareTo(s2));//返回字典序差值
System.out.println(s1.compareTo(s3));//相同返回0
System.out.println(s1.compareTo(s4));//前几个字符相同,返回字符串长度只差
}
}

4. int compareToIgnoreCase(String str) 方法:与compareTo不同的是忽略大小写进行比较
public class Main {
public static void main(String[] args) {
String s1 = "Hello";
String s2 = "hi";
String s3 = "hello";
String s4 = "helloaaa";
System.out.println(s1 == s2);//两个字符串不一样
System.out.println(s3 == s1);//虽然两个字符串相同,但是指向的引用不同
System.out.println(s1 == s4);//指向同一个引用
System.out.println(s1.equals(s3));//两个字符串相同
System.out.println(s2.equals(s1));//两个字符串不同
System.out.println(s1.compareTo(s2));//返回字典序差值
System.out.println(s1.compareTo(s3));//相同返回0
System.out.println(s1.compareTo(s4));//前几个字符相同,返回字符串长度只差
System.out.println(s1.compareToIgnoreCase(s3));//忽略大小写进行比较
}
}

字符串查找也是字符串中非常常见的操作,String类提供了如下查找方法:

public class Main {
public static void main(String[] args) {
String s = "aaabbbcccddd";
System.out.println(s.charAt(2));
System.out.println(s.indexOf('a'));
System.out.println(s.indexOf('a',2));
System.out.println(s.indexOf("bb"));
System.out.println(s.indexOf("bb",4));
System.out.println(s.lastIndexOf('d'));
System.out.println(s.lastIndexOf('d',10));
System.out.println(s.lastIndexOf("dd"));
System.out.println(s.lastIndexOf("dd",10));
}
}

public class Main {
public static void main(String[] args) {
String s1 = String.valueOf(12);
String s2 = String.valueOf(12.12);
String s3 = String.valueOf(true);
System.out.println(s1);
System.out.println(s2);
System.out.println(s3);
int a = Integer.parseInt(s1);
double b = Double.parseDouble(s2);
System.out.println(s1);
System.out.println(s2);
}
}

2. 大小写转换
public class Main {
public static void main(String[] args) {
String s1 = "me";
String s2 = "YOU";
System.out.println(s1.toUpperCase());
System.out.println(s2.toLowerCase());
}
}

注意:这里在转化的是一个新的字符串,不是在原来的字符串上修改,因为字符串具有不可变性
3. 字符串转数组和字符串转数组
public class Main {
public static void main(String[] args) {
String s1 = "abcdefg";
char[] array = s1.toCharArray();
System.out.println(Arrays.toString(array));
String s2 = new String(array);
System.out.println(s2);
}
}

4. 格式化字符串
public class Main {
public static void main(String[] args) {
String s = String.format("%d-%d-%d",2024,3,11);
System.out.println(s);
}
}
