String类①
一、常用方法
-
字符串构造
// 使用常量串构造 String str1 = "hello world"; // 直接newString对象 String str2 = new String("hello world"); // 使用字符数组进行构造 char[] array = {'h','e','l','l','o',' ','w','o','r','l','d'}; String str3 = new String(array);
-
字符串的比较
2.1 ==:比较是否引用同一个对象
注:对于内置类型,==比较的是变量中的值;对于引用类型==比较的是引用中的地址。
public static void main(String[] args) { int a = 9; int b = 99; int c = 9; // 对于基本类型变量,==比较两个变量中存储的值是否相同 System.out.println(a == b); // false System.out.println(a == c); // true String str1 = new String("hello"); String str2 = new String("hello"); String str3 = new String("world"); String str4 = str1; // 对于引用类型变量,==比较两个引用变量引用的是否为同一个对象 System.out.println(str1 == str2); // false System.out.println(str2 == str3); // false System.out.println(str1 == str4); // true }
2.2 boolean equals(Object anObject) 方法:按照字典序比较(字典序:字符大小的顺序)
String类重写了父类Object中equals方法,Object中equals默认按照==比较,String重写equals方法后,按照
如下规则进行比较
public boolean equals(Object anObject) { // 1. 先检测this 和 anObject 是否为同一个对象比较,如果是返回true if (this == anObject) { return true; } // 2. 检测anObject是否为String类型的对象,如果是继续比较,否则返回false if (anObject instanceof String) { // 将anObject向下转型为String类型对象 String anotherString = (String)anObject; int n = value.length; // 3. this和anObject两个字符串的长度是否相同,是继续比较,否则返回false if (n == anotherString.value.length) { char v1[] = value; char v2[] = anotherString.value; int i = 0; // 4. 按照字典序,从前往后逐个字符进行比较 while (n-- != 0) { if (v1[i] != v2[i]) return false; i++; } return true; } } return false; }
public static void main(String[] args) { String s1 = new String("hello"); String s2 = new String("hello"); String s3 = new String("Hello"); // s1、s2、s3引用的是三个不同对象,因此==比较结果全部为false System.out.println(s1 == s2); // false System.out.println(s1 == s3); // false System.out.println(s1.equals(s2)); // 虽然s1与s2引用的不是同一个对象,但是两个对象中放置的内容相同,因此输出true System.out.println(s1.equals(s3)); // s1与s3引用的不是同一个对象,而且两个对象中内容也不同,因此输出false }
2.3 int compareTo(String s) 方法:按照字典序进行比较
与equals不同的是,equals返回的是boolean类型,而compareTo返回的是int类型。具体比较方式:
-
先按照字典次序大小比较,如果出现不等的字符,直接返回这两个字符的大小差值
-
如果前k个字符相等(k为两个字符长度最小值),返回值两个字符串长度差值
public static void main(String[] args) { String s1 = new String("abc"); String s2 = new String("ac"); String s3 = new String("abc"); String s4 = new String("abcdef"); System.out.println(s1.compareTo(s2)); // 不同输出字符差值-1 System.out.println(s1.compareTo(s3)); // 相同输出 0 System.out.println(s1.compareTo(s4)); // 前k个字符完全相同,输出长度差值 -3 }
2.4 int compareToIgnoreCase(String str) 方法:与compareTo方式相同,但是忽略大小写比较
public static void main(String[] args) { String s1 = new String("abc"); String s2 = new String("ac"); String s3 = new String("ABc"); String s4 = new String("abcdef"); System.out.println(s1.compareToIgnoreCase(s2)); // 不同输出字符差值-1 System.out.println(s1.compareToIgnoreCase(s3)); // 相同输出 0 System.out.println(s1.compareToIgnoreCase(s4)); // 前k个字符完全相同,输出长度差值 -3 }
-
-
字符串查找
方法 功能 char charAt(int index) 返回index位置上字符,如果index为负数或者越界,抛出 int indexOf(int ch) 返回ch第一次出现的位置,没有返回-1 int indexOf(int ch, int fromIndex) 从fromIndex位置开始找ch第一次出现的位置,没有返回-1 int indexOf(String str) 返回str第一次出现的位置,没有返回-1 int indexOf(String str, int fromIndex) 从fromIndex位置开始找str第一次出现的位置,没有返回-1 int lastIndexOf(int ch) 从后往前找,返回ch第一次出现的位置,没有返回-1 int lastIndexOf(int ch, int fromIndex) 从fromIndex位置开始找,从后往前找ch第一次出现的位置,没有返回-1 int lastIndexOf(String str) 从后往前找,返回str第一次出现的位置,没有返回-1 int lastIndexOf(String str, int fromIndex) 从fromIndex位置开始找,从后往前找str第一次出现的位置,没有返回-1 public static void main(String[] args) { String s = "aaabbbcccaaabbbccc"; System.out.println(s.charAt(6)); // 'c' System.out.println(s.indexOf('b')); // 3 System.out.println(s.indexOf('c', 12)); // 15 System.out.println(s.indexOf("bbb")); // 3 System.out.println(s.indexOf("bbb", 10)); // 12 System.out.println(s.lastIndexOf('b')); // 14 System.out.println(s.lastIndexOf('c', 10)); // 8 System.out.println(s.lastIndexOf("bbb")); // 12 System.out.println(s.lastIndexOf("bbb", 10)); // 3 }
-
字符串替换
方法 功能 String replaceAll(String regex, String replacement) 替换所有的指定内容 String replaceFirst(String regex, String replacement) 替换首个内容 String str = "helloworld" ; System.out.println(str.replaceAll("o", "O")); //hellOwOrld System.out.println(str.replaceFirst("l", "_")); //he_loworld
-
字符串拆分
方法 功能 String[] split(String regex) 将字符串全部拆分 String[] split(String regex, int limit) 将字符串以指定的格式,拆分为limit组 String str = "hello world Hello World" ; String[] result1 = str.split(" ") ; // 按照空格拆分 for(String s: result1) { System.out.print("_"+s); }//_hello_world_Hello_World String[] result2 = str.split(" ",2) ; // 按照空格拆分为两组 for(String s: result2) { System.out.print("_"+s); }//_hello_world Hello World
-
字符串截取
方法 功能 String substring(int beginIndex) 从指定索引截取到结尾 String substring(int beginIndex, int endIndex) 截取部分内容(前闭后开区间) String str = "helloworld" ; System.out.println(str.substring(5));//world System.out.println(str.substring(0, 5));//hello