String类常用方法:
String s1=“HelloJava”;
String s2=“HelloJSP”;
字符串值的字符个数
System.out.println(s1.length()); //9
比较两个字符串是否相同
System.out.println(s1.equals(s2)); //false
字符串替换
System.out.println(s1.replace('l', 'p')); //HeppoJava
System.out.println(s1.replace("Java", "World")); //HelloWorld
返回指定字符串指定位置的字符
System.out.println(s1.charAt(6)); //a
按字典顺序比较字符串大小
System.out.println("a".compareTo("z")); //-25
判断字符串是否以指定的参数结尾
System.out.println(s2.endsWith("JSP")); //true
判断字符串是否以指定的参数开头
System.out.println(s2.startsWith("Hello")); //true
将基本数据类型转换为字符串(int 转 String)
System.out.println(String.valueOf(10)+5); //105
判别字符串是否为空
System.out.println("".isEmpty()); //true
返回指定字符序列在字符串中的索引
System.out.println(s1.indexOf('l')); //2 第一次出现的位置索引
System.out.println(s1.indexOf("lo")); //3 取指定字符序列的第1个字符
返回指定字符序列在字符串中最后出现的索引
System.out.println(s1.lastIndexOf("l"));//3 最后出现的索引
从指定索引位置截取字符串
System.out.println(s1.substring(s1.indexOf("J"))); //Java
System.out.println(s1.substring(s1.indexOf("lo"), s1.indexOf("va"))); //loJa
System.out.println(s1.substring(2,8)); //lloJav