Java中常用的工具类——字符串类

提示:字符串类是编程中最常用的一种数据类型,比较重要❗


前言

提示:这里可以添加本文要记录的大概内容:

  • Java中的字符串类是java.lang.String。它提供了许多方法来处理字符串,例如截取、拼接、替换、比较等等。
  • 字符串类是不可变的,这意味着一旦创建了一个字符串对象,就不能更改字符串的内容。如果需要更改字符串,必须创建一个新的字符串对象。
  • Java中的字符串是 Unicode 字符序列,可以包含任何字符,包括汉字和特殊字符。
  • 字符串可以通过使用单引号或双引号来创建。使用双引号创建的字符串可以包含空格和特殊字符,使用单引号创建的字符串只能包含一个字符。
  • Java中有许多其他类也可以处理字符串,例如StringBuffer和StringBuilder。这些类提供了更多的方法来修改字符串,但是它们不是线程安全的,所以在多线程环境中使用时要小心。

在这里插入图片描述


提示:以下是本篇文章正文内容,下面案例可供参考

一、字符串类创建对象方式

静态创建

  1. 静态创建:直接使用"字符串内容"

静态方式创建的字符串对象,存储在字符串常量池中

相同内容的字符串对象在常量池中只会创建一个对象
原因:尽可能节省内存空间

public static void main(String[] args) {
    
    
	String s1 = "汤姆要";
	String s2 = "汤姆要";
	System.out.println(s1==s2);
}

在这里插入图片描述

动态创建

  1. 动态创建:使用new构造方法

动态方式创建的字符串存储在堆区
每创建一个都要占据一个新空间

public static void main(String[] args) {
    
    
	String s1 = new String("抓杰瑞");
	String s2 = new String("抓杰瑞");		
	System.out.println(s1==s2);
}

二、String字符串内容不可改变

String 字符串对象创建后内容不能改变
一旦改变时间上产生一个新的字符串

public static void main(String[] args) {
    
    
	String s1 = "汤姆要";
	String s2 = "抓杰瑞";
	s1 = s1+s2;
	System.out.println(s1);	
}

在这里插入图片描述

三、字符串常用方法

length方法

返回一个字符串长度

在Java中,字符串类中的 length() 方法返回字符串的长度。它返回一个 int 值,表示字符串中字符的数量。

例如,下面的代码返回字符串 “Hello, World!” 的长度:

public static void main(String[] args) {
    
    
	String str = "Hello, World!"; 
	System.out.println(str.length());  // 13
}

在这个例子中,字符串 str 的长度是 13,因为它包含 13 个字符(包括空格和标点符号)。

需要注意的是,length() 方法返回的是字符串的长度,而不是容量。字符串的容量是指底层字符数组大小,而长度是指字符串中实际字符的数量。

charAt方法

获取指定下标位置的字符,下标从0开始

public static void main(String[] args) {
    
    
	String str = "hello world";
	System.out.println(s.charAt(3));
}

substring方法

获取指定下标范围的子串

public static void main(String[] args) {
    
    
	String str = "hello world";
	//substring(int beginIndex,int endIndex) 包括开始,不包括结束
	System.out.println(str.substring(1,6));  //ello 
	//从下标为1的位置开始截取,一直到下标为6的位置(不包含6)
	
	System.out.println(str.substring(6));  //world
	//substring(int beginIndex) 从开始到结束
}

indexOf与lastIndexOf

在Java中,indexOf和lastIndexOf方法可以用来搜索字符串中指定字符或子字符串的位置,它们的用法如下:

indexOf:获取某个指定字符或字符串第一次出现的下标位置
lastIndexOf:获取某个指定字符或字符串最后一次出现的下标位置

indexOf方法

获取某个指定字符或字符串第一次出现的下标位置

如果搜索到指定的字符或子字符串,则返回该字符或子字符串所在位置的下标值
返回值为-1,则代表此字符或字符串没有出现

public static void main(String[] args) {
    
    
	String str = "hello world";
	System.out.println(str.indexOf('y'));  //单个字符''
	//输出-1
}

调用indexOf方法中结果为-1表示未查询到指定的字符或字符串。

搜索指定字符

public static void main(String[] args) {
    
    
	String str = "hello world";
	System.out.println(str.indexOf('h'));  //单个字符''
	//输出0
}

搜索指定字符串

public static void main(String[] args) {
    
    
	String str = "hello world";
	System.out.println(str.indexOf("world")); //"字符串"
	//输出6
}

在以上示例中,调用indexOf方法分别搜索字符’h’和字符串"world"在原始字符串中第一次出现的位置,最终返回结果为0和6

lastIndexOf方法

lastIndexOf方法与indexOf方法类似,不同的是它是获取某个指定字符或字符串最后一次出现的下标位置

public static void main(String[] args) {
    
    
	String str = "hello world";	
	System.out.println(str.lastIndexOf('o')); //搜索指定字符
	System.out.println(str.lastIndexOf("wor")); //搜索指定字符串
}

在以上示例中,调用lastIndexOf方法分别搜索字符’o’和字符串"wor"在原始字符串中最后一次出现的位置,最终返回结果为7和6

替换字符串或字符

在Java中,有多种方法可以替换字符串或字符,这里介绍其中两种比较常见的方式:

他并不是真的替换了,而是返回了新的串,因为字符串不可改变

replace(char a,char b)将字符串中出现的a替换成b
replaceAll(String a,String b)将字符串中出现的a替换成字符串b

replace方法

replace方法用于替换字符串中的指定字符或字符串,它有两个参数,第一个参数为要被替换的字符或字符串,第二个参数为替换后的新字符或新字符串。

方法签名如下:

public static void main(String[] args) {
    
    
	String str = "hello world";	
	//替换指定字符,将字符串中的所有'o'字符替换成'*'字符
	System.out.println(str.replace('o', '*')); 
	//替换指定字符串,将字符串中的"world"字符串替换成"Java"字符
	System.out.println(str.replaceAll("world", "Java")); 
}

在以上示例中,调用replace方法分别替换字符串中的’o’字符和"world"字符串,返回替换后的新字符串

replaceAll方法

replaceAll方法用于替换字符串中的匹配正则表达式的部分,它有两个参数,第一个参数为要被替换部分的正则表达式,第二个参数为替换后的新字符串。

例如:

String str = "123 abc 456 def";
String newStr = str.replaceAll("\\d+", "X"); //将字符串中所有的数字都替换成'X'字符

System.out.println(newStr); //输出X abc X def

在以上示例中,调用replaceAll方法使用正则表达式"\d+"匹配字符串中所有的数字,将其替换成'X'字符,输出替换后的新字符串。

比较字符串内容

equals方法,是严格区分大小写的比较
equalsIgnoreCase 方法,不区分大小写比较

实际应用中比较字符串内容是否相等,必须使用equals

在Java中比较字符串内容的常用方法有以下几种:

equals方法

equals方法是String类中用来比较字符串内容是否相同的方法,它比较字符串对象中的字符序列是否相等。
如果相等,返回true;否则返回false。

例如:

String str1 = "hello";
String str2 = "hello";
String str3 = "Hello";

System.out.println(str1.equals(str2)); //输出true
System.out.println(str1.equals(str3)); //输出false

在以上示例中,使用equals方法比较两个字符串对象的内容,结果str1和str2内容相同,返回true;而str1和str3内容不同,返回false。

equalsIgnoreCase方法

equalsIgnoreCase方法和equals方法相似,唯一不同的是它忽略字符大小写。
如果忽略大小写后两个字符串内容相等,返回true;否则返回false。

例如:

String str1 = "hello";
String str2 = "HELLO";

System.out.println(str1.equalsIgnoreCase(str2)); //输出true

在以上示例中,使用equalsIgnoreCase方法比较两个字符串对象的内容,忽略了字符大小写,结果返回true。

大小写转换

toUpperCase 将小写字母转换成大写

toLowerCase 将大写字母转换成小写

在Java中,可以使用以下方法将字符串中的字符转换为大写或小写:

toUpperCase方法

toUpperCase方法是String类中的方法,用于将字符串中的所有小写字母转换成大写字母。方法返回一个新的字符串对象,原始字符串不发生改变。

例如:

public static void main(String[] args) {
    
    
	String str = "hello world";
	System.out.println(str.toUpperCase()); //输出"HELLO WORLD"
}

在以上示例中,使用toUpperCase方法将字符串str中的所有小写字母转换为大写字母,并将结果保存在一个新的字符串对象中。

toLowerCase方法

toLowerCase方法是String类中的方法,用于将字符串中的所有大写字母转换成小写字母。方法返回一个新的字符串对象,原始字符串不发生改变。

例如:

public static void main(String[] args) {
    
    
	String str = "HELLO WORLD";
	System.out.println(str.toLowerCase()); //输出"hello world"
}

在以上示例中,使用toLowerCase方法将字符串str中的所有大写字母转换为小写字母,并将结果保存在一个新的字符串对象中。

前缀和后缀

startsWith:判断字符串是否以指定的字符串开头

endsWith:判断字符串是否以指定的字符串结尾

Java字符串中的前缀和后缀是指字符串开头和结尾的部分。
可以使用String类的startsWith()方法来判断一个字符串是否以指定的前缀开始,使用endsWith()方法来判断一个字符串是否以指定的后缀结束

例如:

public static void main(String[] args) {
    
    
	String str = "Hello, World!";
    System.out.println(str.startsWith("World!")); //false
    System.out.println(str.endsWith("World!")); //true
}

猜你喜欢

转载自blog.csdn.net/rej177/article/details/130956331