String字符串的常见方法总结

一、获取字符串信息

1.获取字符串长度 length()
String str="We are student";
//获取字符串长度
int size=str.length();
System.out.println(size);//14
2.查找字符串 indexOf()
  • indexOf():返回的是搜索的字符或字符串首次出现的位置
  • lastIndexOf():返回的是搜索的字符或字符串最后一次出现的位置
  • 注:没查找到就返回-1
public class demo01 {
	public static void main(String[] args) {
		String str="We are student";
		//查找字符第一次出现的位置
		int index=str.indexOf("e");
		System.out.println(index);//1
		//查找字符最后一次出现的位置
		int lastindex=str.lastIndexOf("e");
		System.out.println(lastindex);//11
	}	
}
3.获取指定下标位置的字符 charAt()
public static void main(String[] args) {
	String str="We are student";
	char a=str.charAt(8);
	System.out.println(a);//t
}

二、字符串操作

1.获取子字符串 subString()
subString(int beginindex)beginindex:从某一索引处开始截取
subString(int beginindex,it endindex)beginindex:从某一索引处开始截取
beginindex:从某一索引处结束截取(不包括)
public class demo01 {
	public static void main(String[] args) {
		String str="We are student";
		//从指定位置截取至字符串结束
		String a=str.substring(3);
		System.out.println(a);//are student
		//从指定位置(包括)截取至另一指定位置(不包括)
		String b=str.substring(3, 6);
		System.out.println(b);//are
	}	
}
2.去除空格 trim()
public class demo01 {
	public static void main(String[] args) {
		String str=" We are student ";
		System.out.println("字符串原来长度"+str.length());//字符串原来长度16
		//将str去掉前、后空格
		System.out.println("去掉空格后长度"+str.trim().length());//去掉空格后长度14
	}	
}
3.替换字符串 replace()
public class demo01 {
	public static void main(String[] args) {
		String str="abcdefg";
		String a=str.replace("a", "A");
		System.out.println(a);//Abcdefg
	}	
}
4.判断字符串的开始与结尾
startsWith()判断当前字符串对象的前缀是否为参数指定的字符串
endsWith()判断当前字符串对象是否以参数指定的字符串结束
public class demo01 {
	public static void main(String[] args) {
		String str="abcdefg";
        //判断是否以"a"开始
		boolean a=str.startsWith("a");
		System.out.println(a);//true
        //判断是否以"G"结束
		boolean b=str.endsWith("G");
		System.out.println(b);//false
	}	
}
5.判断字符串是否相等
equals()比较两字符串是否相等
equalsIgnoreCase()忽略大小写,比较两字符串是否相等
public class demo01 {
	public static void main(String[] args) {
		String str="abcdefg";
		String ing="ABCDEFG";
		boolean a=str.equals(ing);
		boolean b=str.equalsIgnoreCase(ing);
		System.out.println(str+" "+"equals"+" "+ing+":"+a);
        //abcdefg equals ABCDEFG:false
		System.out.println(str+" "+"equalsIgnoreCase"+" "+ing+":"+b);
        //abcdefg equalsIgnoreCase ABCDEFG:true
	}	
}
6.比较两个字符串 compareTo()

a.compareTo(b):

  • 如果a<b:比较结果为一个负整数;
  • 如果a>b:比较结果为一个正整数;
  • 如果a=b:比较结果为0。
public class demo01 {
	public static void main(String[] args) {
		String str1="a";
		String str2="b";
		int a=str1.compareTo(str2);
		System.out.println(str1+" "+"compareTo"+" "+str2+":"+a);
		//a compareTo b:-1
	}	
}
7.字符串中字母大小写的转换
toLowerCase()将字符串中的所有大写字母转换为小写
toUpperCase()将字符串中的所有小写字母转换为大写
public class demo01 {
	public static void main(String[] args) {
		String str="We Are Student";
		String ret1=str.toLowerCase();
		String ret2=str.toUpperCase();
		System.out.println(ret1);//we are student
		System.out.println(ret2);//WE ARE STUDENT
	}	
}
8.分割字符串 split()

将字符串按指定的分割字符进行分割,并将分割后的结果存储在字符串数组中。

import java.util.Arrays;

public class demo01 {
	public static void main(String[] args) {
		String str="192.168.0.1";
		//方式1:
		//将分割后的结果存储在字符串类型的数组
		String[] arr=str.split("\\.");
		//输出全部分割结果
		System.out.println(Arrays.toString(arr));//[192, 168, 0, 1]
		//方式2:
		System.out.println(Arrays.toString(str.split("\\.")));//[192, 168, 0, 1]
	}	
}
9.格式化字符串 format()
public class demo01 {
	public static void main(String[] args) {
		//格式化拼接
		String ret=String.format("商品为%s,价格为%.2f,代言人为%s", "烤椰拿铁",9.9,"易烊千玺");
		System.out.println(ret);//商品为烤椰拿铁,价格为9.90,代言人为易烊千玺
	}	
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值