java String 类中的常用方法

public class Test{
	public static void main(String[] args){
		//1、字符串转换为char
		char c = "胶带".charAt(1);
		System.out.println(c);						//带
		//2、字符串之间比较大小,左小右大负数,左大右小正数
		int retValue = "abc".compareTo("abd");
		System.out.println(retValue);				//-1
		//3、判断前面的字符串中是否包含后面的子字符串
		boolean b = "abc".contains("a");
		System.out.println(b);						//true
		//4、判断当前字符串是否以某个字符串结尾
		boolean c2 = "abc".endsWith("c");
		System.out.println(c2);						//true
		//5、判断两个字符串是否相等,并且忽略大小写
		boolean d = "Abc".equalsIgnoreCase("aBc");
		System.out.println(d);						//true
		//6、将字符串转换成字节数组
		byte[] bytes = "abc".getBytes();
		for(byte  b2 : bytes){
			System.out.println(b2);					//97  98  99
		}
		//7、判断某个子字符串在当前字符串中第一次出现处的索引
		int retValue2 = "abcb".indexOf("b");
		System.out.println(retValue2);				//1
		//8、判断某个字符串是否为空
		boolean e = "".isEmpty();
		System.out.println(e);						//true
		//9、判断字符串长度
		int retValue3 = "abc".length();
		System.out.println(retValue3);				//3
		//10、判断某个字符串在当前字符串中最后一次出现的索引
		int retValue4 = "abcb".lastIndexOf("b");
		System.out.println(retValue4);				//3
		//11、将字符串中的某个字符换成别的字符
		String s = "aaabbbccc".replace("a","b");
		System.out.println(s);						//bbbbbbccc
		//12、拆分字符串
		String[] strs = "a-b-c".split("-");
		for(String s2 : strs){
			System.out.println(s2);					//a   b   c
		}
		//13、判断某个字符串是否以某个字符串开始
		boolean f = "abc".startsWith("a");			//true
		System.out.println(f);
		//14、截取字符串*开始下标,结束下标
		String s2 = "abcd".substring(1,3);			//bc
		System.out.println(s2);
		//15、将字符串转换成char数组
		char[] chars = "abc".toCharArray();
		for(char c3 : chars){
			System.out.println(c3);					//a   b  c
		}
		//16、转换成小写
		String s3 = "ABC".toLowerCase();
		System.out.println(s3);						//abc
		//17、转换成大写
		String s4 = "abc".toUpperCase();			//ABC
		System.out.println(s4);
		//18、去除字符串前后空白,不去中间空白
		String s5 = "   a b c   ".trim();			//a b c
		System.out.println(s5);
		//19、将非字符串转换为字符串
		String s6 = String.valueOf(123);
		System.out.println(s6);						//123
	}
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

生活低手

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值