字符串的比较
1. equals() 方法
java
// 语法:字符串1.equals(字符串2) // 功能:比较两个字符串的内容是否完全相同(区分大小写) // 返回值:boolean(true表示相同,false表示不同) String str1 = "Hello"; String str2 = "Hello"; String str3 = "hello"; String str4 = new String("Hello"); // 比较内容 boolean result1 = str1.equals(str2); // true boolean result2 = str1.equals(str3); // false(大小写不同) boolean result3 = str1.equals(str4); // true(内容相同) // 错误示例:使用 == 比较字符串内容 System.out.println(str1 == str2); // true(指向常量池同一对象) System.out.println(str1 == str4); // false(不同对象,地址不同)2. equalsIgnoreCase() 方法
java
// 语法:字符串1.equalsIgnoreCase(字符串2) // 功能:比较两个字符串的内容是否相同,忽略大小写 // 返回值:boolean String str1 = "Hello"; String str2 = "hello"; String str3 = "HELLO"; String str4 = "World"; boolean result1 = str1.equalsIgnoreCase(str2); // true boolean result2 = str1.equalsIgnoreCase(str3); // true boolean result3 = str1.equalsIgnoreCase(str4); // false
3. 比较注意事项
java
// 1. 空指针安全比较 String str = null; // 错误:会产生空指针异常 // if (str.equals("test")) { } // 正确1:将已知字符串放前面 if ("test".equals(str)) { System.out.println("相等"); } // 正确2:使用Objects.equals()(Java 7+) if (Objects.equals(str, "test")) { System.out.println("相等"); } // 2. 比较前处理 String input = " Hello "; String expected = "hello"; // 去除空白并统一大小写比较 boolean isMatch = input.trim().equalsIgnoreCase(expected); // true字符串遍历
1. charAt() 方法
java
// 语法:字符串.charAt(索引) // 功能:返回指定索引处的字符 // 索引范围:0 到 字符串长度-1 String str = "Hello"; char ch1 = str.charAt(0); // 'H' char ch2 = str.charAt(1); // 'e' char ch3 = str.charAt(4); // 'o' // 索引越界会抛出异常 // char ch4 = str.charAt(5); // StringIndexOutOfBoundsException
2. length() 方法
java
// 语法:字符串.length() // 功能:返回字符串的长度(字符个数) // 注意:与数组的 length 属性不同,这是方法! String str1 = "Hello"; String str2 = ""; String str3 = "你好世界"; // 中文字符每个算一个长度 int len1 = str1.length(); // 5 int len2 = str2.length(); // 0 int len3 = str3.length(); // 4 // 对比数组长度 int[] arr = {1, 2, 3}; int arrLength = arr.length; // 属性,不是方法3. 字符串遍历示例
示例1:键盘录入并遍历字符串
java
import java.util.Scanner; public class StringTraversal { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("请输入一个字符串:"); String input = scanner.nextLine(); System.out.println("遍历结果:"); // 方法1:使用for循环遍历 for (int i = 0; i < input.length(); i++) { char ch = input.charAt(i); System.out.println("索引 " + i + " 的字符是:" + ch); } System.out.println("字符串长度:" + input.length()); } }示例2:统计字符串中的字符类型
java
public class CharStatistics { public static void main(String[] args) { String str = "Hello123@World!"; int letterCount = 0; // 字母计数 int digitCount = 0; // 数字计数 int otherCount = 0; // 其他字符计数 // 遍历字符串 for (int i = 0; i < str.length(); i++) { char ch = str.charAt(i); if (Character.isLetter(ch)) { letterCount++; } else if (Character.isDigit(ch)) { digitCount++; } else { otherCount++; } } System.out.println("字符串:" + str); System.out.println("字母个数:" + letterCount); System.out.println("数字个数:" + digitCount); System.out.println("其他字符个数:" + otherCount); } }示例3:字符串反转
java
public class StringReverse { public static void main(String[] args) { String original = "Hello World"; String reversed = ""; // 方法1:从后往前遍历 for (int i = original.length() - 1; i >= 0; i--) { reversed += original.charAt(i); } System.out.println("原始字符串:" + original); System.out.println("反转后字符串:" + reversed); // dlroW olleH // 方法2:使用StringBuilder(效率更高) StringBuilder sb = new StringBuilder(original); String reversed2 = sb.reverse().toString(); System.out.println("使用StringBuilder反转:" + reversed2); } }示例4:查找字符串中的特定字符
java
public class FindCharacter { public static void main(String[] args) { String str = "programming"; char target = 'g'; System.out.println("在字符串 \"" + str + "\" 中查找字符 '" + target + "':"); // 查找所有出现位置 for (int i = 0; i < str.length(); i++) { if (str.charAt(i) == target) { System.out.println("找到字符 '" + target + "' 在索引 " + i); } } // 统计出现次数 int count = 0; for (int i = 0; i < str.length(); i++) { if (str.charAt(i) == target) { count++; } } System.out.println("字符 '" + target + "' 出现次数:" + count); } }重要注意事项
1. 字符串长度与数组长度的区别
java
// 数组:使用 length 属性 int[] arr = new int[5]; int arrLen = arr.length; // 属性,不加括号 // 字符串:使用 length() 方法 String str = "Hello"; int strLen = str.length(); // 方法,加括号
2. 遍历时的边界检查
java
String str = "Hello"; // 安全的遍历方式 for (int i = 0; i < str.length(); i++) { char ch = str.charAt(i); // 安全的索引访问 // 处理字符 } // 不安全的方式(容易出错) int len = str.length(); for (int i = 0; i <= len; i++) { // 错误:应该是 i < len // char ch = str.charAt(i); // 最后一次循环会索引越界 }3. 空字符串处理
java
String str = ""; // 空字符串,长度为0 // 空字符串的遍历 if (str.length() == 0) { System.out.println("字符串为空"); } else { for (int i = 0; i < str.length(); i++) { char ch = str.charAt(i); // 处理字符 } }4. 使用增强for循环遍历字符数组
java
// 如果需要频繁访问,可以先转换为字符数组 String str = "Hello"; char[] chars = str.toCharArray(); // 使用增强for循环遍历 for (char ch : chars) { System.out.println(ch); } // 使用普通for循环 for (int i = 0; i < chars.length; i++) { System.out.println("索引 " + i + ": " + chars[i]); }总结要点
比较字符串内容:必须使用
equals()或equalsIgnoreCase(),不要用==获取字符串长度:使用
length()方法,不是属性遍历字符串:使用
charAt(index)配合length()方法索引范围:从 0 到
length()-1,注意边界检查处理空字符串:遍历前检查长度是否为0
public class StringDemo1 { public static void main(String[] args) { //创建两个字符串对象 String s1 = new String("abc"); String s2 = "abc"; String s3 = "ABc"; //==号比较 //基本数据类型:比较的是数据值 //引用数据类型:比较的是地址值 System.out.println(s1==s2); //比较字符串中的内容是否相等 boolean result1 = s1.equals(s2); boolean result2 = s1.equals(s3); System.out.println(result1); System.out.println(result2); //比较内容是否相等,忽略大小写 boolean result3 = s1.equalsIgnoreCase(s3); System.out.println(result3); } }import java.util.Scanner; public class StringDemo2 { public static void main(String[] args) { //假设键盘录入一个abc Scanner Sc = new Scanner(System.in); System.out.println("请输入一个字符串:"); String str1 = Sc.next(); //自定义一个字符串abc String str2 = "abc"; //用==比较 System.out.println(str1==str2); } }import java.util.Scanner; public class StringDemo3 { public static void main(String[] args) { //登陆 String user = "yuqi"; String password = "123456"; Scanner Sc = new Scanner(System.in); for (int count = 1;count <= 3;count++) { System.out.println("请输入用户名:"); String User = Sc.next(); System.out.println("请输入密码:"); String Password = Sc.next(); if (user.equals(User) && password.equals(Password)) { System.out.println("登陆成功"); break; } else { if (count==3) { System.out.println("账号被锁定"); }else { System.out.println("账号或密码错误,登陆失败。你还有"+(3-count)+"次机会"); } } } } }import java.util.Scanner; public class StringDemo4 { public static void main(String[] args) { //键盘录入一个字符串,统计大写字母字符,小写字母字符,数字字符出现的次数 Scanner Sc = new Scanner(System.in); System.out.println("请输入一个字符串:"); String str = Sc.next(); int bigCount = 0; int smallCount = 0; int numberCount = 0; //i依次表示字符串中的每一个索引 for (int i = 0;i < str.length();i++){ char c = str.charAt(i); if (c >= 'a' && c <= 'z'){ smallCount++; } else if (c >= 'A' && c <= 'Z') { bigCount++; } else if (c >= '0' && c <= '9') { numberCount++; } } System.out.println("大写字母出现"+bigCount+"次"); System.out.println("小写字母出现"+smallCount+"次"); System.out.println("数字出现"+numberCount+"次"); } }