(1)定义
-----------Sting类,定义在java.lang包下
1、String的注意点:
字符串的内容是不可变的,它的对象在创建后不能被更改(但是可以创建一个新的字符串,赋值给原 来的字符串变量)
2、创建String对象:
(1)直接赋值;String s = "abc";
以下是使用String类的构造方法来创建字符串对象的:
(2)new + 空参构造 String s1 = new String();
(3)new + 有参构造 Strint s2 = new Stirng(s)/String s2 =new String("abc");
(4)new + 有参构造(字符数组)
char[] chs = {'a','b','c','d','e'};
String s3 = new String(chs);
(5)new + 有参构造(字节数组)
byte[] bytes = {97,98, 99,100,101};
String s4 = new String(bytes);
System.out.println(s4);//输出结果为abcde;
(6)截取字符串String (char a[],int offset,int length):
该方法是截取字符数组a中一部分字符串对象,offset表示开始截取字符串的位置,length表示截取字符串的长度;
Char a[] = {'s','t','u','d','e','n','t'};
String s =new String(a,2,4);等价于String s = new String("uden");
(2)String 类中常见的成员方法
-1、连接多个字符串
其实就是前面的“+”运算符。
String s1 = new String("明明就");
String s2 = new String("不喜欢我");
String s = s1 + s2;
System.out.println("你" + s);
就相当于:System.out.println("你" + s1 + s2);
-2、获取字符串信息、
(1)获取字符串长度
Stirng str = "you don`t love me";
Int size = str.length();
使用 String类中的length()方法来获取字符串对象的长度。
注:length()返回的字符串长度包括字符串中的空格。
(2)查找字符串
———1、indexOf (String s) ;
indexOf(String s);
该方法会返回参数字符串s在它指定字符串中首次出现的索引位置。
当调用String类中的indexOf()方法时,会从当前字符串的开始位置处搜索s的位置。indexOf()方法如果没有检索到字符串s,则返回-1,语法如下:
str.indexOf(substr);
str:任意字符串对象;
substr:要搜索的字符串;
查找字符m在字符串str中的索引位置:
String str = "you don`t love me";
Int size = str.indexOf("o");
———2、lastindexOf(String str);
lastindexOf(String str);
该方法用于返回指定字符串最后一次出现的索引位置。
当调用String类的lastIndexOf()方法时,会从当前字符串的开始位置处检索字符串参数str,并将最后一次出现str的索引位置进行返回。如果没有检索到字符串str,则返回-1.语法:
str.lastIndexOf(substr);
注:如果lastIndexOf()_方法中的参数是空字符串,则该方法返回的结果与调用length()方法的返回结果相同。
String s = "abc";
长度:s.length() = 3
System.out.println(s.lastIndexOf(""));
// 输出 3
System.out.println(s.length());
// 输出 3
(3)获取指定索引位置的字符
charAt();
使用charAt()方法可将指定索引处的字符进行返回。
str.charAt(int index);
str:任意字符串
index:整型值,用于指定要返回字符的下标
public class text4 {
public static void main(String[] args) {
String str = "hello world";
char mychar = str.charAt(6);
System.out.println(mychar);输出结果为 w
}
}
-3、字符串操作
(1)获取子字符串
———1、substring(int beginIndex);
substring()方法
String类中的substring()方法可以对字符串进行截取,有两种不同的形式重载
该方法是从指定的索引位置开始截取直到字符串的结尾子串。
str.substring(int beginIndex);
String str = "hello world";
String substr = str.substring(3);//输出为lo world;
———2、substring (int beginIndex,int endIndex);
该方法返回的是从字符串某一索引位置开始截取至某一索引位置结束的子串。
beginIndex:开始截取子字符串的索引位置
endIndex:子字符串在整个字符串中的结束位置
iiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii特别注意:
取左不取右,中间全部要,即实际字符串是beginIndex到endIndex-1.
public class text4 {
public static void main(String[] args) {
String str = "《将进酒》:李白(唐)";
String substr = str.substring(6,8);
System.out.println(substr);输出结果是李白(6和7)
}
}
(2)去除空格
str.trim()
trim()方法返回字符串的副本,忽略前导空格和尾部空格
str.trim()忽略str中的前导和尾部空格
public class text4 {
public static void main(String[] args) {
String str = " you don`t love me ";
System.out.println(str.length()); 长度位20
System.out.println(str.trim().length()); 长度为17
}
}
(3)替换字符串(或字符)
replace()
replace()方法可实现将指定的字符或字符串替换成新的字符或字符串
语法:str.replace(CharSequence target,CharSequence replacement);
target:要替换的字符或字符串
replacement:用于替换原来字符串的内容
replace()方法返回的结果是一个新的字符串。如果字符或字符串oldChar没有出现在该对象表达式的字符串序列中,则将原字符串进行返回。
public class text4 {
public static void main(String[] args) {
String str = "you don`t love me";
String newstr = str.replace("don`t","");
System.out.println(newstr); 输出为you love me这里是把don`t替换成空
String str2 = "you don`t love me";
String newstr2 =str2.replace("m","h"); 输出为you don`t love he
System.out.println(newstr2);
}
}
注:如果要替换的字符串oldChar在字符串中重复出现多次,那么replace()方法会将所有的oldChar方法全部替换成newChar。
public class text4 {
public static void main(String[] args) {
String str = "you don`t love your dear me";
String newstr = str.replace("you","You");
System.out.println(newstr); //输出为 You don`t love Your dear me
}
}
(4)判断字符串的开始与结尾
———1、startsWith(String prefix)
startsWith()方法
该方法用于判断当前字符串对象的前缀是否为参数指定的字符串,返回值类型为boolean类型
语法:str.startsWith(String prefix);
prefix是指作为前缀的字符串
———2、endsWith(String suffx)
endsWith()方法
该方法用于判断当前字符串是否为以给定的子字符串结束。返回值类型为boolean类型
语法:str.endsWith(String suffx);
suffx是指作为后缀的字符串
public class text4 {
public static void main(String[] args) {
String str1 = "250040011";
String str2 = "240040012";
boolean b1 = str1.startsWith("25");
boolean b2 = str1.endsWith("12");
boolean b3 = str2.startsWith("25");
boolean b4 = str2.endsWith("12");
System.out.println("字符串str1是以‘25'开始的吗?" + b1);
System.out.println("字符串str1是以‘12'结束的吗?" + b2);
System.out.println("字符串str2是以'25'开始的吗?" + b3);
System.out.println("字符串str2是以‘12'结束的吗?" + b4);
}
}
输出为:
字符串str1是以‘25'开始的吗?true
字符串str1是以‘12'结束的吗?false
字符串str2是以'25'开始的吗?false
字符串str2是以‘12'结束的吗?true
(5)判断字符串是否相等
1、引入:"=="比较运算符是比较两个字符串的地址是否相同。即使两个字符串的内容相同,两个对象的地址也是不同的,使用比较运算符仍然会返回false。如果是比较引用数据类型,那么==会比较变量的内存地址。
2、“==”和equals()
== 作用:永远比较的是【值】
- 如果是基本类型(int, double…):比较数据值
- 如果是引用类型(对象、String…):比较地址值
equals() 作用:默认比较地址,重写后比较内容
- 像
String、Integer、自定义类都重写了 equals → 比较内容 - 没重写的类 → 和
==一模一样
———1、equals(String orherstr);
equals()
如果两个字符串具有相同的字符和长度,则使equals()方法进行比较时,返回true;否则,返回false,返回的boolean类型。
语法:str.equals(String otherstr);
其中str、otherstr是要比较的两个字符串对象;
———2、equalsIgnoreCase(String otherstr)
equalsIgnoreCase()
使用equals()方法对字符串进行比较时是区分大小写的,而使用equalsIgnoreCase()方法是在忽略了大小写的情况下比较两个字符串是否相等的,返回结果仍为Boolean类型。
语法:str.equalsIgnoreCase(String otherstr);
public class text4 {
public static void main(String[] args) {
String str1 = "She don`t love me";
String str2 = "she don`t love me";
boolean b1 = str1.equals(str2);
boolean b2 = str1.equalsIgnoreCase(str2);
System.out.println("在区分大小写的情况下" + b1);
System.out.println("在忽略大小写的情况下" + b2);
以下是输出结果:
在区分大小写的情况下false
在忽略大小写的情况下true
}
}
(6)按字典顺序比较两个字符串
compareTo()
从起始位置开始逐位比较字符和C语言中的strcmp(s1,s2)一样;
语法 :str.compareTo(String otherstr);
如果str字符序列大于otherstr则返回一个正整数
如果str小于otherstr则返回一个负整数
否则返回0;
(7)字母大小写的转换
1、toLowerCase()
语法:str.toLowerCase()
该方法将str字符串中的所有大写字母转换为小写,如果没有可以被转换的字母则返回原字符串,否则返回一个新的字符串。
String newstr1 = str.toLowerCase();
2、toUpperCase()
语法:str.toUpperCase()
该方法将str字符串中的所有小写字母转换为大写,如果没有可以被转换的字母则返回原字符串,否则返回一个新的字符串。
String newstr2 = str.toUpperCase();
(8)分割字符串
———1、split(String sign)
split();
使用该方法可以使字符串按照指定的字符或字符串进行分割,并将分割后的结果存储在字符串数组中,split()有两种不同形式;
语法:str.split(String sign);
其中sign为分割字符串的分割符,也可以使用正则表达式。
注:如果想定义多个分割符,可以使用符号"|",例如",|="表示分割符为“,”和“=”
public class SplitTest {
public static void main(String[] args) {
String str = "张三,李四,王五,赵六";
// 按逗号分割
String[] arr = str.split(",");
// 遍历输出
for (String s : arr) {
System.out.println(s);
}
}
}
输出结果:
张三
李四
王五
赵六
———2、split(String sign,int limit)
该方法可根据给定的分割符对字符串进行拆分,并限定拆分的次数。
语法:str.split(String sign,int limit);
其中limit为分割次数;
public class SplitTest {
public static void main(String[] args) {
String str = "张三,李四,王五,赵六";
// 按逗号分割,最多分成 2 个元素
String[] arr = str.split(",", 2);
for (String s : arr) {
System.out.println(s);
}
}
}
张三
李四,王五,赵六













