Java 中字符串的串联
字符串串联是合并两个或多个小字符串以创建最终字符串的操作。例如,您可以通过连接此人的名字和姓氏来创建全名。Java 提供了几种连接字符串的方法,但其中最简单的方法是使用 + 运算符。 有 在 Java 中连接字符串的四种方法:- 串联运算符 '+'
- 函数 String.concat()
- StringBuilder
- StringBuffer
使用运算符连接字符串 +
您可以使用运算符 '+' 连接两个或多个字符串,这是完成工作的最简单方法。例如,One” + Two” 将生成 String 对象 OneTwo”。您可以使用此运算符组合至少两个 String,例如:ab”+cde”+fghi 将给出 ”abcdefghi,也可以使用 String variables.
编译器将此操作转换为:
编译器将此操作转换为:
new StringBuilder().append(”string1).append(”string2).toString()此程序连接两个字符串:
public class Concatenation_string {Runtime:
public static void main(String[] args) {
String s1 = string1”;
字符串 s2 = string2”;
字符串 s1s2 = concatenation_strings_java(s1, s2);
System.out.println(s1s2);
}
public static String concatenation_strings_java (String s1, String s2) {
return s1+s2;
}
}
string1chain2
用 String.concat()
此方法将一个字符串连接到另一个字符串的末尾。该方法返回一个 String,其值为 String 在串联方法中传递,其末尾用于调用该方法。public String concat(String s)Example:
public class Concatenation_string {Runtime:
public static void main(String[] args) {
字符串 s = 我是一体的”;
s = s.concat(程序员”);
System.out.println(s);
}
}
我是程序员
'+' 运算符和 String.concat()
为了能够区分 '+' 和 concat(),我们将使用一个实际的例子。a+=b首先,语义上有细微的差异。如果 a 是 null,则 a.concat(b) 触发 NullPointerException,但 '+' 在语句中 a+=b 处理 a即使它是 null。此外,concat() 仅接受 String 值,而 '+' 运算符使用 toString() )。我们可以说 concat() 更严格,因为它接受.
a.concat(b)
你可以编写这段代码,看看你在输出中得到什么:
public static void main(String[] args) {你应该在运行时有这个:
String s = null;
s = s.concat(程序员”);
System.out.println(s);
}
线程 main” java.lang.NullPointerException与 '+':
at Concatenation_string.main(Concatenation_string.java:5)
public static void main(String[] args) {你应该得到这样的结果:
String s = null;
s = s + 程序员;
System.out.println(s);
}
nullprogrammer所以,正如我们之前所说,运算符 '+' 等效于:
a = new StringBuilder()
.append(a)
.append(b)
.toString();
使用 StringBuffer 和 StringBuilder
这种串联方式可以减少内存和时间成本,因为它不会生成 String 对象。事实上,您应该始终使用 StringBuilder 而不是 StringBuffer,因为它具有相同的功能,但没有同步,这意味着操作更快。使用 StringBuffer 或 StringBuilder 也会生成结构化代码,如下所示:String result = new StringBuilder(15).append(name).append( ”).append(firstname).toString();你可以看到使用 StringBuilder 连接多个字符串是多么容易。但是,请记住使用等于最终字符串中的字符数的容量初始化 StringBuilder。这将通过正确初始化我们的 String 来防止丢失不可用的内存并减少 CPU 时间。
这是一个 java 程序的示例,它将向您展示如何将 String 与 StringBuilder 和 StringBuffer 连接起来。
public static void main(String[] args) {输出:
String court =Court of”;
字符串 module_math =math”;
/* StringBuilder */
String module_res = networks”;
int length = court.length()+module_math.length();
StringBuilder sb = new StringBuilder(length);
sb.append(court).append(module_math);
字符串 res = sb.toString();
System.out.println(res);
/* StringBuilder */
length = court.length()+module_math.length();
StringBuffer sBuffer = new StringBuffer(length);
sb.append(和”).append(court).append(module_res);
res = sb.toString();
System.out.println(res);
}
Math Courtyard
Math Courtyard and Network Courtyard
Performance of String Concatenation in Java
很明显,连接字符串的最简单、最快捷的方法是使用+”运算符当你只有两个字符串时,它工作得很好,但如果你需要连接数千个字符串,尤其是在一个循环中,那么性能会随着+”运算符的下降而降低。性能不佳的原因是为 String 对象创建了临时批处理。如果你的程序中有很多字符串,你可以看到下面的结果,这些结果表明大规模串联的最佳方法是使用 StringBuilder。这些统计数据是在 Windows 7 64 位环境中进行的,Core 2 Duo 处理器 (2.53 GHz) 和 4 GB RAM.public static void main(String[] args) {10000 个串联操作的结果:
int nb_op=10000;
字符串 s=”;
double t1,t2,time;
t1= System.currentTimeMillis();
for(int i = 0 ; i < nb_op; i++)
s += *”;
t2 = System.currentTimeMillis();
时间 = t2-t1;
System.out.println(+: ”+时间);
s=”;
t1= System.currentTimeMillis();
for(int i = 0 ; i < nb_op; i++)
s.concat(*”);
t2 = System.currentTimeMillis();
时间 = t2-t1;
System.out.println(concat: ”+时间);
StringBuilder sb=new StringBuilder();
t1= System.currentTimeMillis();
for(int i = 0 ; i < nb_op; i++)
sb.append(*”);
t2 = System.currentTimeMillis();
时间 = t2-t1;
System.out.println(StringBuilder: ”+时间);
StringBuffer sBuffer=new StringBuffer();
t1= System.currentTimeMillis();
for(int i = 0 ; i < nb_op; i++)
sBuffer.append(*”);
t2 = System.currentTimeMillis();
时间 = t2-t1;
System.out.println(StringBuffer: ”+时间);
}
+: 180.0
concat: 13.0
StringBuilder: 2.0
StringBuffer: 4.0
![]() |
10000 次操作的时间(以毫秒为单位) |
第一印象太棒了!在这里,我们立即注意到运算符+”实现的时间与串联操作的数量成正比,并且它已经完成了 100000 次操作的 10 秒。因此,在处理大型项目时,+”运算符是一个非常糟糕的选择。concat、StringBuilder 和 StringBuffer 对 '+' 运算符不可见,但您可以在统计表中看到 StringBiulder 花费的时间更少。
方法 concat() 速度很快。但是,就性能而言,使用 StringBuilder 方法可以更快地完成多个字符串的串联。
Sun JDK 中提供了 String 和 StringBuilder 的源代码。你可以看到你正在建造一个坦克板。它的内存分配非常快。
记住:
- 永远不要在循环中使用运算符 +。
- 始终使用 StringBuilder 连接多个 Strings。
- 始终初始化 StringBuilder.
Java - String concat() method
字符串串联: concat() vs + operator
Java - String Buffer append() 方法