字符串是编程中最基础且重要的数据类型,掌握其特性与操作技巧对提升开发效率至关重要。本文将详细介绍Java字符串的实现原理与实用技巧。
作为由零个或多个字符组成的有限序列,字符串在编程中被广泛使用。其核心特性在于不可变性,任何修改操作都会创建新对象而非改变原内容。

Java通过java.lang.String类实现字符串功能,并采用常量池机制优化存储效率。
直接使用双引号创建字符串:
String str1 = "Hello";
使用new关键字创建字符串对象:
String str2 = new String("World");
通过字符数组创建字符串:
char[] charArray = {'J', 'a', 'v', 'a'};
String str3 = new String(charArray);
获取字符串长度:
int length = str1.length();
字符串连接:
String combined = str1.concat(str2);
字符串比较:
boolean isEqual = str1.equals(str2);
boolean ignoreCase = str1.equalsIgnoreCase("hello");
字符串截取:
String sub = str1.substring(1, 3);
查找字符或子串:
int index = str1.indexOf('e');
boolean contains = str1.contains("ell");
将基本类型转换为字符串:
String numStr = String.valueOf(123);
将字符串转换为基本类型:
int num = Integer.parseInt("456");
double d = Double.parseDouble("3.14");
对于频繁修改字符串的场景,使用StringBuilder(非线程安全)或StringBuffer(线程安全):
StringBuilder sb = new StringBuilder();
sb.append("Hello");
sb.append(" ");
sb.append("World");
String result = sb.toString();
使用String.format()方法进行格式化:
String formatted = String.format("Name: %s, Age: %d", "Alice", 25);
使用printf风格格式化:
System.out.printf("Value: %.2f%n", 3.14159);
使用正则表达式匹配:
boolean matches = "123-45-6789".matches("d{3}-d{2}-d{4}");
使用正则表达式分割字符串:
String[] parts = "apple,orange,banana".split(",");
指定字符编码转换:
byte[] utf8Bytes = str1.getBytes(StandardCharsets.UTF_8); String decoded = new String(utf8Bytes, StandardCharsets.UTF_8);
字符串匹配是计算机科学中的一个基础问题,指在一个主字符串(文本)中查找一个子字符串(模式)是否出现及出现的位置。该问题广泛应用于文本编辑、生物信息学、数据检索等领域。
KMP算法(Knuth-Morris-Pratt算法)是一种高效的字符串匹配算法,其核心思想是通过预处理模式串(Pattern)构建部分匹配表(Partial Match Table,简称PMT),利用已匹配的信息避免不必要的回溯。
public class KMP {
// 构建部分匹配表(next数组)
private static int[] buildNext(String pattern) {
int[] next = new int[pattern.length()];
next[0] = -1; // 初始化
int i = 0, j = -1;
while (i < pattern.length() - 1) {
if (j == -1 || pattern.charAt(i) == pattern.charAt(j)) {
i++;
j++;
next[i] = j;
} else {
j = next[j];
}
}
return next;
}
// KMP匹配算法
public static int kmpSearch(String text, String pattern) {
int[] next = buildNext(pattern);
int i = 0, j = 0;
while (i < text.length() && j < pattern.length()) {
if (j == -1 || text.charAt(i) == pattern.charAt(j)) {
i++;
j++;
} else {
j = next[j];
}
}
return j == pattern.length() ? i - j : -1;
}
public static void main(String[] args) {
String text = "ABABDABACDABABCABAB";
String pattern = "ABABCABAB";
int index = kmpSearch(text, pattern);
System.out.println("匹配起始位置: " + index); // 输出: 10
}
}
ABABCABAB的next数组为[-1, 0, 0, 1, 2, 0, 1, 2, 3]。以文本串ABABDABACDABABCABAB和模式串ABABCABAB为例: