String类型常用方法汇总

字符串的操作在开发过程中使用的比较多,以下是经常用到的一些方法

 
 String demoString = "ceshibaixiaosheng";
		
		//判断字符串是否包含 false
		System.out.println(demoString.contains("t")); 
		
		//字符串相加  也可以使用+
		String dsg = demoString.concat(" very good");
		System.out.println(dsg); //ceshibaixiaosheng very good
	
		//字符串长度 17
		int length = demoString.length();
		System.out.println(length);	
		
		//比较字符串是否相等  false
		System.out.println("baixiaosheng".equals(demoString));
		
		//截取字符串      角标从0开始
		String str1 = demoString.substring(5,8); 
		System.out.println(str1); //bai
		//从5开始到结尾
		String str2 = demoString.substring(5); 
		System.out.println(str2);
		
		//判断是否以某字符串开头
		boolean sw = demoString.startsWith("ceshi");
		System.out.println(sw);
		
		//判断是否以某字符串结尾
		boolean ew = demoString.endsWith("baixiaosheng");
		System.out.println(ew);
		
		//找出字符串在字符串中第一次出现的位置    	如果没有返回-1
		int indexOf = demoString.indexOf("bai"); 
		System.out.println(indexOf); //5
		
		//找出字符串中最后一次出现的位置
		int lastIndex = demoString.lastIndexOf("e");
		System.out.println(lastIndex); //14
		
		//字符串字母都变成大写    CESHIBAIXIAOSHENG
		System.out.println(demoString.toUpperCase());
		
		//字符串字母都变成小写   ceshibaixiaosheng
		System.out.println(demoString.toLowerCase());
		
		//去掉首尾的空格
		System.out.println(" baixiaosheng ".trim());
		
		//替换字符串
		String replace = demoString.replace("ceshi","AA");
		System.out.println(replace); //AAbaixiaosheng
		
		//将字符串中的第一个出现的字符串替换掉
		String replaceF = demoString.replaceFirst("e","3");
		System.out.println(replaceF); //c3shibaixiaosheng
		
		//替换中所有的
		String replaceAll = demoString.replaceAll("e","6");
		System.out.println(replaceAll); //c6shibaixiaosh6ng




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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值