当前位置: 首页 > ai >正文

Java 中 String 类的常用方法

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)格式化字符串(类似 printfString.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() 等便捷方法。

http://www.xdnf.cn/news/15946.html

相关文章:

  • OneCode 3.0 @TreeAnnotation及@ChildTreeAnnotation子树注解速查手册
  • 生存分析机器学习问题
  • 数据交换---JSON格式
  • IDEA-通过IDEA导入第三方的依赖包
  • Android常用的adb和logcat命令
  • Qt/C++源码/监控设备模拟器/支持onvif和gb28181/多路批量模拟/虚拟监控摄像头
  • RedisJSON 指令精讲JSON.TOGGLE 键翻转布尔值
  • Python趣味算法:实现任意进制转换算法原理+源码
  • 【无标题】buuctf-re3
  • 企业级IIS配置手册:安全加固/负载均衡/性能优化最佳实践
  • PyQt5—QLabel 学习笔记
  • 常用 Flutter 命令大全:从开发到发布全流程总结
  • ELF 文件操作手册
  • Java 动态导出 Word 登记表:多人员、分页、动态表格的最佳实践
  • C++11--锁分析
  • ospf技术
  • 【SpringAI实战】实现仿DeepSeek页面对话机器人
  • Jiasou TideFlow AIGC SEO Agent:全自动外链构建技术重构智能营销新标准
  • 技术与情感交织的一生 (十)
  • Spring处理器和Bean的生命周期
  • LinkedList与链表(单向)(Java实现)
  • 【2025/07/21】GitHub 今日热门项目
  • WinForm-免费,可商用的WinForm UI框架推荐
  • Linux 命令大全
  • Three.js实现银河流光粒子星空特效原理与实践
  • 【Android】交叉编译faiss库 | 问题解决
  • 【HarmonyOS】ArkTS语法详细解析
  • C++ <继承> 详解
  • Java IO流体系详解:字节流、字符流与NIO/BIO对比及文件拷贝实践
  • kafka 生产和消费 性能测试工具 kafka-producer-perf-test.sh kafka-consumer-perf-test.sh