字符串(String)在程序设计中被广泛使用,C语言也可以通过字符数组或字符指针的方式来完成字符操作,但是这种做法将数据和操作分离了,不符合面相对象的思想,java专门提供了String及其相关的一系列类来管理字符串.
使用字符串常量构造
String str1 = "hello world";
new String对象

String str2 = new String("hello world");
使用字符数组进行构造

char[] str = {'h','e','l','l','o','w','o','r','l','d'};
String str3 = new String(str);
而String类型是字符串应用类型,所以使用 == 只能比较两个字符串是否引用了同一个对象.
练习:
public class Test {
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
}
}
总结:
String类重写了父类Object中equals方法,Object中equals默认按照==比较,String重写equals方法后,按照字典序进行比较,即字符的大小顺序.
public class Test {
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
}
}
与equals不同的是,equals返回的是boolean类型,而compareTo返回的是int类型。
具体比较方式:
public class Test {
public static void main(String[] args) {
String s1 = new String("abc");
String s2 = new String("ac");
String s4 = new String("abcdef");
System.out.println(s1.compareTo(s2)); // 不同输出字符差值-1
System.out.println(s1.compareTo(s4)); // 前k个字符完全相同,输出长度差值 -3
}
}
与compareTo方式相同,但是忽略大小写比较,这里不作介绍
| 方法 | 功能 |
|---|---|
| char charAt(int index) | 返回index位置上字符,如果index为负数或者越界,抛出IndexOutOfBoundsException异常 |
| int indexOf(int ch) | 返回ch第一次出现的位置,没有返回-1 |
| int indexOf(int ch, intfromIndex) | 从fromIndex位置开始找ch第一次出现的位置,没有返回-1 |
| int indexOf(String str) | 返回str第一次出现的位置,没有返回-1 |
| int indexOf(String str, int fromIndex) | 从fromIndex位置开始找str第一次出现的位置,没有返回-1 |
| int lastIndexOf(int ch) | 从后往前找,返回ch第一次出现的位置,没有返回-1 |
| int lastIndexOf(int ch, int fromIndex) | 从fromIndex位置开始找,从后往前找ch第一次出现的位置,没有返回-1 |
| int lastIndexOf(String str) | 从后往前找,返回str第一次出现的位置,没有返回-1 |
| int lastIndexOf(String str, int fromIndex) | 从fromIndex位置开始找,从后往前找str第一次出现的位置,没有返回-1 |
public class Test {
public static void main(String[] args) {
String s = "aaabbbcccaaabbbccc";
System.out.println(s.charAt(3)); // 'b'
System.out.println(s.indexOf('c')); // 6
System.out.println(s.indexOf('c', 10)); // 15
System.out.println(s.indexOf("bbb")); // 3
System.out.println(s.indexOf("bbb", 10)); // 12
System.out.println(s.lastIndexOf('c')); // 17
System.out.println(s.lastIndexOf('c', 10)); // 8
System.out.println(s.lastIndexOf("bbb")); // 12
System.out.println(s.lastIndexOf("bbb", 10));
}
}
public class Test {
public static void main(String[] args) {
// 数字转字符串
String s1 = String.valueOf(1234);
String s2 = String.valueOf(12.34);
String s3 = String.valueOf(true);
System.out.println(s1);
System.out.println(s2);
System.out.println(s3);
System.out.println("=================================");
// 字符串转数字
int data1 = Integer.parseInt("1234");
double data2 = Double.parseDouble("12.34");
System.out.println(data1);
System.out.println(data2);
}
}
public class Test {
public static void main(String[] args) {
String s1 = "hello";
String s2 = "HELLO";
// 小写转大写
System.out.println(s1.toUpperCase());
// 大写转小写
System.out.println(s2.toLowerCase());
}
}
public class Test {
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);
}
}
public class Test {
public static void main(String[] args) {
String s = String.format("%d-%d-%d", 2019, 9, 14);
System.out.println(s);
}
}

后面的方法就不作代码演示了,感兴趣的可以自己体验
| 方法 | 功能 |
|---|---|
| String[] split(String regex) | 将字符串全部拆分 |
| String[] split(String regex, int limit) | 将字符串以指定的格式,拆分为limit组 |
| 方法 | 功能 |
|---|---|
| String substring(int beginIndex) | 从指定索引截取到结尾 |
| String substring(int beginIndex, int endIndex) | 截取部分内容 |
![]() |
String是一种不可变对象. 字符串中的内容是不可改变.。字符串不可被修改,是因为:

由于String的不可更改特性,为了方便字符串的修改,java中又提供StringBuilder和StringBuffer类。
