Java 的 String
类提供了丰富的方法来操作字符串。由于字符串是 不可变对象(任何修改都会生成新对象),所有方法都不会改变原字符串,而是返回新字符串。以下是 Java String 的常用方法分类及示例:
1. 字符串基本信息
方法 | 功能 | 示例 |
---|
length() | 返回字符串长度 | "hello".length() → 5 |
isEmpty() | 判断字符串是否为空(长度为 0) | "".isEmpty() → true |
isBlank() (Java 11+) | 判断字符串是否为空或仅含空白字符 | " ".isBlank() → true |
String str = "hello";
System.out.println(str.length()); // 5
System.out.println("".isEmpty()); // true
System.out.println(" ".isBlank()); // true (Java 11+)
2. 字符串查找
方法 | 功能 | 示例 |
---|
charAt(int index) | 返回指定索引处的字符 | "hello".charAt(1) → 'e' |
indexOf(String str) | 返回子串首次出现的索引(未找到返回 -1 ) | "hello".indexOf("ll") → 2 |
lastIndexOf(String str) | 返回子串最后一次出现的索引 | "hello".lastIndexOf("l") → 3 |
contains(CharSequence s) | 判断是否包含子串 | "hello".contains("ell") → true |
startsWith(String prefix) | 判断是否以某字符串开头 | "hello".startsWith("he") → true |
endsWith(String suffix) | 判断是否以某字符串结尾 | "hello".endsWith("lo") → true |
java
String s = "hello";
System.out.println(s.indexOf("l")); // 2
System.out.println(s.contains("ell")); // true
System.out.println(s.startsWith("he")); // true
3. 字符串截取与分割
方法 | 功能 | 示例 |
---|
substring(int beginIndex) | 从 beginIndex 开始截取到末尾 | "hello".substring(2) → "llo" |
substring(int begin, int end) | 截取 [begin, end) 的子串 | "hello".substring(1, 4) → "ell" |
split(String regex) | 按正则表达式分割字符串 | "a,b,c".split(",") → ["a", "b", "c"] |
split(String regex, int limit) | 限制分割次数 | "a,b,c".split(",", 2) → ["a", "b,c"] |
String s = "hello,world";
String[] parts = s.split(","); // ["hello", "world"]
String sub = s.substring(1, 4); // "ell"
4. 字符串修改(返回新字符串)
方法 | 功能 | 示例 |
---|
toLowerCase() | 转为小写 | "HELLO".toLowerCase() → "hello" |
toUpperCase() | 转为大写 | "hello".toUpperCase() → "HELLO" |
trim() | 去除首尾空白字符 | " hello ".trim() → "hello" |
strip() (Java 11+) | 去除首尾空白(包括 Unicode 空格) | " hello ".strip() → "hello" |
replace(char old, char new) | 替换字符 | "hello".replace('l', 'L') → "heLLo" |
replace(CharSequence old, CharSequence new) | 替换子串 | "hello".replace("ll", "LL") → "heLLo" |
concat(String str) | 拼接字符串 | "hello".concat(" world") → "hello world" |
String s = " Hello ";
System.out.println(s.trim()); // "Hello"
System.out.println(s.replace("l", "L")); // " HeLLo "
5. 字符串格式化
方法 | 功能 | 示例 |
---|
format(String format, Object... args) | 格式化字符串(类似 printf ) | String.format("Name: %s, Age: %d", "Alice", 25) → "Name: Alice, Age: 25" |
formatted(Object... args) (Java 15+) | 实例方法格式化 | "Name: %s".formatted("Alice") → "Name: Alice" |
String formatted = String.format("Name: %s, Age: %d", "Alice", 25);
System.out.println(formatted); // "Name: Alice, Age: 25"
6. 其他实用方法
方法 | 功能 | 示例 |
---|
toCharArray() | 转为字符数组 | "hello".toCharArray() → ['h', 'e', 'l', 'l', 'o'] |
matches(String regex) | 判断是否匹配正则表达式 | "123".matches("\\d+") → true |
compareTo(String str) | 按字典序比较字符串 | "a".compareTo("b") → -1 |
equals(Object obj) | 判断内容是否相等 | "hello".equals("HELLO") → false |
equalsIgnoreCase(String str) | 忽略大小写比较 | "hello".equalsIgnoreCase("HELLO") → true |
String s = "hello";
System.out.println(s.matches("h.*")); // true
System.out.println(s.compareTo("world")); // -15 (字典序比较)
总结
类别 | 常用方法 |
---|
基本信息 | length() , isEmpty() , isBlank() |
查找 | charAt() , indexOf() , contains() , startsWith() , endsWith() |
截取与分割 | substring() , split() |
修改 | toLowerCase() , toUpperCase() , trim() , replace() , concat() |
格式化 | format() , formatted() |
其他 | toCharArray() , matches() , compareTo() , equals() |
注意:
Java 的 String
是不可变的,所有修改操作都会返回新字符串。
高频操作:split()
、substring()
、replace()
、equals()
。
Java 11+ 新增了 isBlank()
、strip()
、formatted()
等便捷方法。