String类常用操作总结

	
	String file="a.jpg";
		//4,1,2,2,-1
		System.out.println( file.indexOf('g')); //indexof(ch)返回字符所在位置索引下标
		System.out.println(file.indexOf("."));//indexof(str)返回字符所在位置索引下标
		System.out.println(file.indexOf('j', 0));
		System.out.println(file.indexOf("j", 2));//indexof(str,fromindex),从指定索引开始,找字符串第一次出现的下标,
		System.out.println(file.indexOf("j", 3));//找不到返回-1 
		//返回字符串最后出现的索引下标
		System.out.println( file.lastIndexOf("."));
		/**substring:返回一个新字符串,它是此字符串的一个子字符串。该子字符串始于指定索引处的字符,一直到此字符串末尾。
		  参数:beginIndex - 开始处的索引(包括), endIndex - 结束处的索引(不包括)
		如果 beginIndex或endIndex 为负或大于此 String对象的长度,则抛出IndexOutOfBoundsException 
		**/
		System.out.println( file.substring(file.lastIndexOf(".")+1));
	    //返回指定索引处的值
		System.out.println( file.charAt(0));
		//拼接/合并字符串
		System.out.println(file.concat("aaaaa"));
		//是否包含该字符串返回boolean值
		System.out.println(file.contains("jpg"));
		//是否是length为0的空字符串即  ""
		System.out.println(file.isEmpty());
		//是否以img结尾
		System.out.println(file.endsWith("img"));
		//去空格
		System.out.println(file.trim());
		//replace 替换 jpg-被替换为img, .被替换为空
		System.out.println(file.replace("jpg", "img"));
		System.out.println(file.replace(".", ""));
		//比较俩字符串的值,字符比较ASII码,数字比较大小,>1, <-1, =0
		System.out.println(file.compareTo("b.jpg"));
		System.out.println(file.compareTo("a.jpg"));
	
		
		//转为byte字节
		try {
			System.out.println(file.getBytes("UTF-8"));
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
	
		
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值