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();
}
String类常用操作总结
最新推荐文章于 2022-08-15 13:39:57 发布
792

被折叠的 条评论
为什么被折叠?



