7. 基于自动化–Java字符串常用API
一、String类
String s1 = "core Java"; String s2 = "Core Java"; System.out.println("判断两个字符串是否相等:"+s1.equals(s2)); System.out.println("判断两个字符串忽略大小写是否相等:"+s1.equalsIgnoreCase(s2)); System.out.println("判断字符串是否包含查找数据:"+s1.contains("Java")); System.out.println("获取字符串中某个内容第一次出现的索引:"+s1.indexOf("Java")); System.out.println("获取字符串中某个内容最后一次出现的索引:"+s1.lastIndexOf("a")); System.out.println("判断字符串是否为空:"+s1.isEmpty()); System.out.println("截取字符串:"+s1.substring(0,4)); System.out.println("将字符串转换为大写:"+s1.toUpperCase()); System.out.println("将字符串转换为小写:"+s1.toLowerCase()); System.out.println("获取字符串的长度:"+s1.length()); System.out.println("切割字符串:"+s1.split(" ")[1]); System.out.println("获取指定位置上的字符:"+s1.charAt(3));
注意:当对比的其中一个字符串为空时,可以使用Object.equals(str1,str2);