文章目录
- 一:String类
- String的常用API:
- 1.length();求字符串长度。
- 2.charAt(int index);获取索引index的字符。
- 3.toCharArray();将字符串返回一个字符数组。
- 4.indexOf();返回入参字符串在指定的String对象中第一次出现的索引位置,如果查找失败则返回-1。
- 5.contains(CharSequence s) 判断参数字符串是否被包含。
- 6.startsWith() 和 endsWith()
- 7.isEmpty() 判断字符串是否为空。
- 8.valueOf();将入参的类型转为字符串数组,静态方法,直接通过String.valueOf()调用。
- 9.concat(String str);将str连接到指定对象的后面。
- 10.replace(); 字符串替换
- 11.substring();返回指定String对象中的子字符串。(截取)
- 12.split();分割字符串,将字符串按照一定的规则分割成字符串数组。
- 13.toLowerCase();大写转小写。
- 14.toUpperCase();小写转大写。
- 15.trim();返回字符串副本,忽略前导空白和结尾空白。
- 字符串比较:
- 二:StringBuffer类和StringBuilder类(写法一样,以StringBuilder为例)
String类作为引用类型,区别基本数据类型,但是它的使用频率并不低于基本数据类型,下面将对String和StringBuffer、StringBuilder做出介绍。
一:String类
String的常用API:
1.length();求字符串长度。
2.charAt(int index);获取索引index的字符。
3.toCharArray();将字符串返回一个字符数组。
4.indexOf();返回入参字符串在指定的String对象中第一次出现的索引位置,如果查找失败则返回-1。
- indexOf(int ch);
- indexOf(String str);从第一个开始找。
- indexOf(String str,int fromIndex);从索引位置开始找。
- lastindexOf()入参方法与indexOf()类似,但他返回的是入参字符串在指定String对象中最后一次出现的索引位置,查找失败返回-1。
ps:lastindexOf()入参索引是按照反向顺序来搜索的,即如果指定从索引1开始,则只会搜索索引位置1和0。
public class MainClass {
public static void main(String[] args) {
String str = "好好学习天天向上";
System.out.println(str.indexOf("学习"));//2
System.out.println(str.indexOf("玩"));//-1
System.out.println(str.lastIndexOf('学',2));//2
}
}
5.contains(CharSequence s) 判断参数字符串是否被包含。
@Test
public void test(){
String str = "好好学习天天向上";
System.out.println(str.contains("学习"));//true
}
6.startsWith() 和 endsWith()
- startsWith(String prefix) 测试此字符串是否以指定的前缀开始。
- startsWith(String prefix ,int toffset ) 测试此字符串从索引位置开始是否以指定的前缀开始。
- endsWith(String suffix) 测试此字符串是否以指定的后缀结束。
@Test
public void test(){
String str = "好好学习天天向上";
System.out.println(str.startsWith("好"));//true
System.out.println(str.startsWith("天", 4));//true
System.out.println(str.endsWith("上"));//true
}
7.isEmpty() 判断字符串是否为空。
8.valueOf();将入参的类型转为字符串数组,静态方法,直接通过String.valueOf()调用。
public class MainClass {
public static void main(String[] args) {
int a = 1;
String str = new String();
str = String.valueOf(a);
System.out.println(str);// 1
}
}
9.concat(String str);将str连接到指定对象的后面。
public class MainClass {
public static void main(String[] args) {
String str = "好好学习";
str = str.concat("天天向上");
System.out.println(str);//好好学习天天向上
}
}
10.replace(); 字符串替换
- replace(String replacement);替换指定String对象中的字符串序列。
- replace(String replacement)方法不支持正则表达式。
- replaceAll(String regex,String replacement)可以替换指定String对象中所有匹配入参正则表达式的子字符串。
- replaceFirst()可以将“第一个”匹配正则表达式的字符串进行替换。入参方法同上。
public class MainClass {
public static void main(String[] args) {
String str = "好好学习日日学习";
str = str.replace("日日学习","天天向上");
System.out.println(str);//好好学习天天向上
}
}
11.substring();返回指定String对象中的子字符串。(截取)
- substring(int beginIndex);从入参索引位置直到String对象结尾。
- substring(int beginIndex,int endIndex);左闭右开,截取区间内的内容。
public class MainClass {
public static void main(String[] args) {
String str = "好好学习,天天向上";
str = str.substring(1);
System.out.println(str);//好学习,天天向上
str = str.substring(1,2);
System.out.println(str);//学
}
}
12.split();分割字符串,将字符串按照一定的规则分割成字符串数组。
- split(String regex);
- split(String regex,int limit);
public class MainClass {
public static void main(String[] args) {
String str = "好好。学习。天天。向上";
String[] string = str.split("。");
for(String s : string){
System.out.println(s);
}
}
}
好好
学习
天天
向上
13.toLowerCase();大写转小写。
14.toUpperCase();小写转大写。
public class MainClass {
public static void main(String[] args) {
String str = "www.haohaoxuexi.com";
str = str.toUpperCase();
System.out.println(str);//WWW.HAOHAOXUEXI.COM
str = str.toLowerCase();
System.out.println(str);//www.haohaoxuexi.com
}
}
15.trim();返回字符串副本,忽略前导空白和结尾空白。
@Test
public void test(){
String str = " 好好学习天天向上 ";
System.out.println(str);
System.out.println(str.trim());
}
字符串比较:
1.equals() 和 ==
==用来对基本数据类型数据进行比较,String类不是基本类型用“= =”比较String类的对象是比较引用内存地址是否相同,而不是比较两个对象的内容是否一致,所以不能用它来进行字符串比较。
String类中提供equals(String anotherString); equalsIgnoreCase(String anotherString);两种方法来比较字符串内容是否一样,后者不考虑大小写。
public class MainClass {
public static void main(String[] args) {
String str = "www.haohaoxuexi.com";
boolean bool = str.equals("www.haohaoxuexi.com");
System.out.println(bool);//true
bool = str.equalsIgnoreCase("www.HAOHAOXUEXI.COM");
System.out.println(bool);//true
}
}
2.contains(CharSequence s);判断String对象是否包含入参字符串,返回boolean类型;
3.compareTo(String anotherString);按字典序比较两个字符串。
4.compareToIgnoreCase();忽略大小写。
二:StringBuffer类和StringBuilder类(写法一样,以StringBuilder为例)
String、StringBuffer、StringBuilder异同点:
相同点:都是final类,都不能被继承。
不同点:
1、String长度是不可改变的,StringBuffer、StringBuilder长度是可变的。
2、StringBuffer是线程安全(需要加锁,效率低)、StringBuilder是线程不安全(没有加锁,效率高)。
1.append();将字符或字符串添加到StirngBuilder对象的末尾。
public class MainClass {
public static void main(String[] args) {
StringBuilder s = new StringBuilder("www.");
s.append("好好学习");
System.out.println(s);
}
}
www.好好学习
2.insert();将字符或字符串添加到对象的指定位置;
public class MainClass {
public static void main(String[] args) {
StringBuilder s = new StringBuilder("www.");
s.append("好好学习");
System.out.println(s);
s.insert(4,"天天向上");
System.out.println(s);
}
}
www.好好学习
www.天天向上好好学习
3.delete(int start,int end);删除该对象指定索引范围的字符内容;
public class MainClass {
public static void main(String[] args) {
StringBuilder s = new StringBuilder("www.");
s.append("好好学习");
System.out.println(s);
s.insert(4,"天天向上");
System.out.println(s);
s.delete(0,4);
System.out.println(s);
}
}
www.好好学习
www.天天向上好好学习
天天向上好好学习
4.replace(int start,int end,String str);区别于String类中的replace(),StringBuilder类中的replace()可以将指定范围的字符串替换成入参字符串。
public class MainClass {
public static void main(String[] args) {
String str1 = "好好学习天天向上";
StringBuilder str2 = new StringBuilder("好好学习天天向上");
str1 = str1.replace("好好学习","");
str2.replace(0,4,"");
System.out.println(str1+"\n"+str2);
}
}
天天向上
天天向上
5.reverse();将字符串序列反转。
public class MainClass {
public static void main(String[] args) {
String str1 = "好好学习天天向上";
StringBuilder str2 = new StringBuilder("好好学习天天向上");
str1 = str1.replace("好好学习","");
str2.replace(0,4,"");
System.out.println(str1+"\n"+str2);
str2.reverse();
System.out.println(str2);
}
}
天天向上
天天向上
上向天天