Java中字符串基础知识

目录

前言:

一.String类

 1.1声明字符串

1.2创建字符串

二.连接字符串

2.1连接多个字符串

2.2连接其他数据类型

三.获取字符串信息

 3.1获取字符串长度

3.2字符串查找

indexOF(String s)

 lastIndexOF(String str)

3.3获取指定索引位置的字符

四.字符串操作

4.1获取字符串

substring(int beginIndex)

substring(int beginIndex,int endIndex)

4.2去空格

4.3字符串替换

4.4判断字符串的开始与结尾

startsWith()方法

endsWith()方法

4.5判断两个字符串是否相等

equals()方法

equalslgnoreCase()方法

4.6按照字典顺序比较两个字符串

4.7字母大小写转换

4.8字符串分割


前言:

字符串是Java程序中经常处理的对象,字符串作为string类来处理,单个字符串可以用char类型保存,多个字符组成的文本就需要保存在string对象中。

一.String类

 1.1声明字符串

 在Java语言中,字符串必须包含在一对双引号(“”)之内。

格式:

String str;
//String:指定该变量为字符串类型
//str:字符串名称

1.2创建字符串

使用String类的构造方法来创建字符串变量:

String s=new String("good");

引用赋值创建字符串:

String str1,str2;
str1="good boy";
str2="bad boy";

二.连接字符串

2.1连接多个字符串

使用“+”运算符可以实现连接多个字符串的功能,“+”运算符可以连接多个String对象并产生一个新的String对象

 例如:

public class Test {
	
	public static void main(String[] args) {
		String s1=new String("运筹帷幄");
		String s2=new String("科三必过");
		System.out.println(s1+"\n"+s2);
	}

}

2.2连接其他数据类型

如果字符串同其他数据类型数据进行连接,会将其他数据类型的数据直接转换成字符串

例如:

public class Test {
	
	public static void main(String[] args) {
		int booktime=4;
		float practice=2.5f;
		System.out.println("我每天花费"+booktime+"小时看书;"+practice+"时间练习");
	}

}

三.获取字符串信息

 3.1获取字符串长度

 使用String类的length()方法可获取声明的字符串对象的长度

格式:

str.length();
//str为字符串对象

例如:

String str="we are students";
int size=str.length();
//size的值为15(包括字符串中的空格)

3.2字符串查找

indexOF方法返回的是搜索的字符或字符串首次出现的位置,lastIndexOF()方法返回的是搜索的字符或者字符串最后一次出现的位置

indexOF(String s)

用于返回字符串s在指定字符串中首次出现的索引位置,当调用String类的indexOF()方法时,会从当前字符串的开始位置搜索s的位置,如果没有检索到字符串s,则返回-1

格式:

str.indexOF(substr)
//str:字符串对象
//substr:要搜索的字符串

例如:

String str="we are students";
int size=str.indexOF("a");
//size值为3

值得一提的是,String对象是用数组表示的,字符串的下标是0~length()-1。

wearestudents
str的下标01234567891011121314

因此a的下标为3

 lastIndexOF(String str)

该方法用于返回指定字符串最后一次出现的索引位置,将最后一次出现str的索引位置返回,如果没有检索到str,会返回-1。

格式:

str.lastIndexOF(substr)
//str:字符串
//substr:要搜索的字符串

例如:

public class Test {
	
	public static void main(String[] args) {
		String str="we are students";
		int size=str.lastIndexOf("s");
		System.out.println(size);
	}

}

size值为14,尽管students当中有两个s,但是最后一个出现的s在下标为14的位置上

3.3获取指定索引位置的字符

使用charAt()方法可将指定索引处的字符返回

格式:

str.charAt(int index)
//str:字符串
//index:整型值,用于指定要返回字符的下标

例如:

public class Test {
	
	public static void main(String[] args) {
		String str="we are students";
		char mychar=str.charAt(5);
		System.out.println(mychar);
	}

}
//size返回值为e

即返回了下标为5的字符为e

四.字符串操作

4.1获取字符串

substring(int beginIndex)

该方法返回的是从指定的索引位置开始截取直到该字符串结尾的子串

格式:

str.substring(int beginIndex)
//beginIndex:索引某一处

例如:

public class Test {
	
	public static void main(String[] args) {
		String str="we are students";
		String substr=str.substring(3);
		System.out.println(substr);
	}

}
//substr的值为are students
substring(int beginIndex,int endIndex)

该方法返回的是从字符串某一索引位置开始截取到某一索引位置结束的子串

格式:

substring(int beginIndex,int endIndex)
//beginIndex:开始截取子字符串的索引位置
//endIndex:结束位置

例如:

public class Test {
	
	public static void main(String[] args) {
		String str="we are students";
		String substr=str.substring(5,13);
		System.out.println(substr);
	}

}
//输出结果为e studen

4.2去空格

trim()方法返回字符串的副本,忽略前导空格和尾部空格

格式:

str.trim

例如:

public class Test {
	
	public static void main(String[] args) {
		String str="  we are students  ";
		System.out.println(str.length());
		System.out.println(str.trim().length());
	}

}
//19
//15

4.3字符串替换

replace()方法可将指定的字符或者字符串替换为新的字符或字符串

格式:

str.replace(charsequence target,charsequence replacement)
//target:需要替换的字符或字符串
//replacement:用于替换的字符串内容

例如:

public class Test {
	
	public static void main(String[] args) {
		String str="we are students";
		String newstr=str.replace("s","S");
		System.out.println(newstr);
	}

}
//we are StudentS

这里是将所有的s替换成了S

4.4判断字符串的开始与结尾

startsWith()方法和endWIth()方法分别用于判断字符串是否以指定的内容开始或结束,这两个方法的返回值都是boolean类型

startsWith()方法

该方法用于判断当前字符串对象的前缀是否为参数指定的字符串

格式:

str.startsWith(String prefix)
/prefix:作为前缀的字符串
endsWith()方法

该方法用于判断当前字符串是否以给定的子字符串结束

 格式:

str.endsWith(String suffix)
//suffix:作为后缀的字符串

4.5判断两个字符串是否相等

判断两个字符串是否相等不能简单的用“==”,因为比较字符串是否相等比较的是两个字符串的地址是否相等。

equals()方法

如果两个字符串具有相同的字符和长度,则使用equals()方法比较时,返回true,否则返回false

格式:

str.equals(String otherstr)
//str,otherstr是两个要比较的对象
equalslgnoreCase()方法

 使用equals()方法比较字符串的话是要区分大小写的,而使用equalslgnoreCase()方法旨在去除大小写的情况下比较两个字符是否相等,返回值仍为boolean类型

格式:

str.equalslgnoreCase(String otherstr)
//str,otherstr为要比较的两个字符串对象

4.6按照字典顺序比较两个字符串

compareTo()方法为按照字典顺序比较两个字符串,如果按照字典顺序此String对象位于参数字符串之前,则比较结果为一个负整数,之后则为正整数,相等则为0.

格式:

str.compareTo(String otherstr)
//str,otherstr为要比较的两个字符串对象

例如:

public class Test {
	
	public static void main(String[] args) {
		String str1=new String("a");
		String str2=new String("b");
		String str3=new String("c");
		System.out.println("str2与str3相比为:"+str2.compareTo(str3));
		System.out.println("str2与str1相比为:"+str2.compareTo(str1));
	}
}
//-1
//1

4.7字母大小写转换

toLowerCase()方法将字符串中的所有大写字母转换为小写

格式:

str.toLowerCase()

toUpperCase()方法将字符串中的所有小写字母转换为大写

格式:

str.toUpperCase()

4.8字符串分割

split()方法可以将字符串按照指定的分割字符或字符串进行分割,并将分割好的结果存放在字符串数组中。

格式:

str.split(String sign)
//sign:分割字符串的分割符

 split(String sign,int limit)该方法可以根据给定的分割符对字符串进行拆分,并限定拆分的次数

格式:

str.split(String sign,int limit)
//limit:限制次数

  • 6
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值