
在Java中,String类型数据属于引用类型数据,有三种构造方法:
public class Main {
public static void main(String[] args) {
String str1 = "Hello"; //直接以常量赋值
System.out.println(str1);
String str2 = new String("World"); //新建一个String对象
System.out.println(str2);
char[] arr = {'H','i'};
String str3 = new String(arr); //以字符数组创建String对象
System.out.println(str3);
}
}注意:
String怎么进行比较呢?由于String类属于引用类型的数据,相当于C语言的指针,它存放的是地址,所以需要用其他方式来进行比较。
Java中的String类有三种比较方式:
由于String类数据是引用类型,存放是地址,所以使用 “==” 来比较地址是否相等,返回值是布尔值。
public static void main(String[] args) {
String str1 = new String("Hello");
String str2 = new String(" World");
String str3 = s1;
//使用“==”比较的是地址,因为str1,str2,str3均是引用类型
System.out.println(str1 == str2);
System.out.println(str1 == str3);
System.out.println(str2 == str3);
}
//输出结果
false
true
false在Java中,比较String类的引用内容是否相等时通常使用equals方法,返回值是布尔值。
public static void main2(String[] args) {
String str1 = new String("Hello");
String str2 = new String(" World");
String str3 = str1;
//使用equals方法比较的是字符串内容
System.out.println(str1.equals(str2));
System.out.println(str1.equals(str3));
System.out.println(str2.equals(str3));
}
//输出结果
false
true
false当我们需要比较String类引用内容的大小时,通常使用compareTo方法,返回值是两个比较数的差值。
假如要比较str1和str2,调用形式是:str1.compareTo(str2),返回值则是 str1 - str2 的结果。
public static void main(String[] args) {
//根据字母顺序进行比较:b < c,返回负数
String s1 = new String("abb");
String s2 = new String("abc");
System.out.println(s1.compareTo(s2));
System.out.println();
//若不忽略大小写,会根据Unicode码值进行比较
String s3 = new String("abcd");
String s4 = new String("ABCD");
System.out.println(s3.compareTo(s4));
System.out.println();
//忽略大小写,则判断为相等
String s5 = new String("abcd");
String s6 = new String("ABCD");
System.out.println(s5.compareToIgnoreCase(s6));
System.out.println();
//若对应字符都相等,根据长度来比较
String s7 = new String("abcdef");
String s8 = new String("abcde");
System.out.println(s7.compareTo(s8));
}在Java中,一般使用for循环对字符串进行遍历,具体实现方法如下:
public static void main(String[] args) {
String str = "abcdefg";
//charAt方法:返回对应下标的字符
char ch = str.charAt(3); //表示返回字符串中下标为3的字符
System.out.println(ch);
//通过charAt方法遍历输出字符串
for(int i = 0; i < str.length(); i++) {
System.out.print(str.charAt(i)+" ");
}
}字符串的查找方法有两种:indexOf 和 lastIndexOf。
public static void main(String[] args) {
String str = "abcdefg";
//indexOf方法:返回指定字符第一次出现的索引,没有该字符则返回-1
System.out.println(str.indexOf('d'));
System.out.println(str.indexOf('h'));
System.out.println(str.indexOf('g',3)); //表示从下标为3的字符开始查找
System.out.println(str.indexOf("abc"));
System.out.println(str.indexOf("abc",3));
System.out.println();
//lastIndexOf方法:从后往前找指定字符并返回第一次出现的索引,没有该字符则返回-1
System.out.println(str.lastIndexOf('d'));
System.out.println(str.lastIndexOf('h'));
System.out.println(str.lastIndexOf('g',3)); //表示从下标为3的字符开始查找
System.out.println(str.lastIndexOf("abc"));
System.out.println(str.lastIndexOf("abc",3));
}public static void main(String[] args) {
//数字转字符串
String str = String.valueOf(123) + 1; //此处“+ 1”为字符串拼接而非加法运算
System.out.println(str);
System.out.println();
//字符串转数字
int data1 = Integer.parseInt("123") + 1; //此处“+ 1”为加法运算
System.out.println(data1);
double data2 = Double.parseDouble("12.34") + 0.5;
System.out.println(data2);
} public static void main(String[] args) {
//大小写转换
String s1 = "hello";
String s2 = "WORLD";
System.out.println(s1.toUpperCase());//小写转大写
System.out.println(s2.toLowerCase());//大写转小写
System.out.println();
}public static void main(String[] args) {
//字符串转数组
String s3 = "hello";
char[] arr = s3.toCharArray();
for (int i = 0; i < arr.length; i++) { //输出转换后的字符数组
System.out.print(arr[i] + " ");
}
System.out.println();
//数组转字符串
char[] arr2 = {'h','e','l','l','o'};
String s4 = new String(arr2);
System.out.println(s4);
System.out.println();
//格式化
String s5 = String.format("%d + %d == %d",1,1,2);
System.out.println(s5);
}注意,字符串类型是不可变类型,因此所有的替换操作均是在新建的对象上进行修改的,而非在原字符串上进行替换。
//所有字符串替换操作均不在原字符串修改,而是新生成字符串
public static void main(String[] args) {
String str = "Hello World";
//替换所有指定字符或字符串
String s1 = str.replace('l','L');
System.out.println(s1);
String s2 = str.replace("ll","ww"); //只替换从0位置开始第一次出现的对应字符串
System.out.println(s2);
String s3 = str.replaceAll("ll","ww"); //替换字符串当中所有的对应字符串
System.out.println(s3);
//只替换第一个出现的指定字符串
String s4 = str.replaceFirst("l","L");
System.out.println(s4);
}在Java中,我们一般使用 str.split() 来实现字符串的拆分,括号内可以指定以什么来拆分,如以空格来拆分就是:split(" ") (注意引号内有一个空格),返回的字符串可以放入字符串数组。
public static void main(String[] args) {
String str = "Hello World";
//按照空格拆分字符串,返回的两个字符串存入字符串数组
String[] sarr = str.split(" ");
for(int i = 0; i < sarr.length; i++) {
System.out.print(sarr[i] + " ");
}
System.out.println();
sarr = str.split(" ",2); //表示拆成2组
for (int i = 0; i < sarr.length; i++) {
System.out.print(sarr[i] + " ");
}
System.out.println();
/*
* 注意:
* 字符 "|","*","+" 都得加上转义字符,前面加上 "\\"
* 如果是 "\" ,那么就得写成 "\\\\"
* 如果一个字符串中有多个分隔符,可以用"|"作为连字符: str.split("-|=") 表示以 '-' 和 '=' 分割,不分顺序
*/
}我们通常使用 substring 方法和 trim 方法来截取字符串。
具体操作看以下例子:
public static void main(String[] args) {
String str = "Hello World";
String s1 = str.substring(2,5); //表示截取位置为[2,5)的字符,即'llo'
System.out.println(s1);
System.out.println();
//trim方法:去掉字符串开头和结尾的空白字符(空格, 换行, 制表符等)
str = " Hello World ";
System.out.println(str);
String s2 = str.trim();
System.out.println(s2);
}我们前面说到,String类型数据不可以被修改,那么我就是想要修改,有什么方法呢?
Java提供了两个可被修改的String类:StringBuilder和StringBuffer。
若单纯使用String类引用修改字符串,我们需要新建一个对象然后将两个字符串拼接起来,效率较低:
//较低效(使用String)
//原因:String类无法在原来的对象上进行修改,只能新建对象,这将会耗费时间
public static void main(String[] args) {
String str = "Hello";
str += " World";
System.out.println(str);
}使用StringBuilder可以直接在原字符串上修改,大大提高了效率:
//较高效(使用StringBuilder)
//原因:StringBuilder允许在原来的对象上进行修改,省去了很多步骤,节省时间提高效率
public static void main(String[] args) {
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("Hello"); //在末尾添加
stringBuilder.append(" World");
String str = stringBuilder.toString(); //转换成String类
System.out.println(str);
}那有同学问了:这两个有什么区别呀?
区别就是:StringBuilder用于单线程开发,而StringBuffer用于多线程开发。
可以理解为:你一个人编写程序的话用StringBuilder就够了,如果你要和别人合作完成一个项目的话,用StringBuffer会比较安全。
以下例子是它们的两个用法:
public static void main(String[] args) {
//1.逆置.reverse()
StringBuilder stringBuilder1 = new StringBuilder("Hello World");
stringBuilder1.reverse();
System.out.println(stringBuilder1);
//2.插入.insert()
StringBuilder stringBuilder2 = new StringBuilder("Hello World");
stringBuilder2.insert(3,"good"); //表示在位置3前插入
System.out.println(stringBuilder2);
}完