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

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.dtcms.com/a/293032.html

相关文章:

  • JavaScript的进阶学习--函数和基本对象的解析
  • 16-MSTP
  • 加速度计输出值的正负号与坐标系正方向相反
  • 基于 Agent 的股票分析工具
  • Windows Server 设置MySQL自动备份任务(每日凌晨2点执行)
  • 洛谷刷题7..22
  • 贪心算法Day3学习心得
  • VBScript 拖拽文件显示路径及特殊字符处理
  • gitlab私服搭建
  • 根据数据,判断神经网络所需的最小参数量
  • 如何搭建appium工具环境?
  • 嵌入式学习-土堆目标检测(2)-day26
  • 浏览器解码顺序xss
  • UE5 UI WarpBox 包裹框
  • Leetcode力扣解题记录--第41题(原地哈希)
  • 【Pytest】从配置到固件的使用指南
  • 【工作常用】C++/QT插件编程思想——即插即用
  • Elasticsearch 学习笔记
  • 从零开始学习 NumPy 库:核心功能与实践指南
  • 应用层攻防启示录:HTTP/HTTPS攻击的精准拦截之道
  • AI视频-剧本篇学习笔记
  • 《AR眼镜上声学的应用与挑战》
  • pytorch中的torch.compile是如何加速vLLM大模型推理的?
  • 信息学奥赛一本通 1553:【例 2】暗的连锁
  • 跨境企业破局国际市场:海外媒体发稿如何为品牌声誉赋能?
  • 蔚来汽车视觉算法面试30问全景精解
  • 原型链污染
  • 【Phenix】使用教程1|使用phenix.map_model_cc进行结构验证|整体结构CC计算/单个氨基酸的CC
  • Windows入侵排查入门实例
  • 前端_CSS复习