方式一:+ 最常见的方式
String aa = "今天";
String bb = "明天";
System.out.println(aa+bb);
方式二:StringBuilder.append()和StringBuffer.append()
先有StringBuffer后有StringBuilder,两者就像是孪生双胞胎,该有的都有,只不过大哥StringBuffer,大部分方法都经过synchronized修饰,所以StringBuffer是线程安全的,但是它效率就相对StringBuilder较低
String aa = "今天";
String bb = "明天";
StringBuilder sber = new StringBuilder();
StringBuffer sbf = new StringBuffer();
sber.append(aa).append(bb);
System.out.println(sber.toString());
sbf.append(aa).append(bb);
System.out.println(sbf.toString());
方式三:String类下的cocat()方法
如果拼接的字符串是null,concat会抛出NullPointerException。如果拼接的字符串是一个空字符串(“”),那么concat的效率要更高。如果拼接的字符串非常多,concat的效率就会下降,因为创建的字符串对象越多,开销越大。
String aa = "今天";
String bb = "明天";
String concat = aa.concat(bb);
System.out.println(concat);
方式四:String类下的join()方法
JDK1.8提供了一种新的字符串拼接姿势:String类增加了一个静态方法join,第一个参数为字符串连接符
String aa = "今天";
String bb = "明天";
String join = String.join("-", aa, bb);
System.out.println(join);
方式五:StringJoiner
StringJoiner是JDK1.8,java.util包中的一个类,用于构造一个由分隔符重新连接的字符序列
String aa = "今天";
String bb = "明天";
StringJoiner sj = new StringJoiner(":", "[", "]");
sj.add(aa).add(bb);
System.out.println(sj.toString());
StringJoiner源码
package java.util;
public final class StringJoiner {
private final String prefix;//前缀
private final String delimiter;//间隔符
private final String suffix;//后缀
private StringBuilder value;//值
private String emptyValue;//空值
public StringJoiner(CharSequence delimiter) {
this(delimiter, "", "");//默认前缀和后缀为"",重载调用
}
public StringJoiner(CharSequence delimiter,
CharSequence prefix,
CharSequence suffix) {
//间隔符,前缀和后缀判断是否为null,null将抛出异常
Objects.requireNonNull(prefix, "The prefix must not be null");
Objects.requireNonNull(delimiter, "The delimiter must not be null");
Objects.requireNonNull(suffix, "The suffix must not be null");
// 成员变量赋值
this.prefix = prefix.toString();
this.delimiter = delimiter.toString();
this.suffix = suffix.toString();
this.emptyValue = this.prefix + this.suffix;//空值被设置为只有前后缀
}
//设置空值,检查是否为null
public StringJoiner setEmptyValue(CharSequence emptyValue) {
this.emptyValue = Objects.requireNonNull(emptyValue,
"The empty value must not be null").toString();
return this;
}
@Override
public String toString() {
if (value == null) {
return emptyValue;//没有值将返回空值或者后续设置的空值
} else {
if (suffix.equals("")) {
return value.toString();//后缀为""直接返回字符串,不用添加
} else {
//后缀不为"",添加后缀,然后直接返回字符串,修改长度
int initialLength = value.length();
String result = value.append(suffix).toString();
// reset value to pre-append initialLength
value.setLength(initialLength);
return result;
}
}
}
初始化,先添加前缀,有了之后每次先添加间隔符,StringBuilder后续append字符串
public StringJoiner add(CharSequence newElement) {
prepareBuilder().append(newElement);
return this;
}
//合并StringJoiner,注意后面StringJoiner 的前缀就不要了,后面的appen进来
public StringJoiner merge(StringJoiner other) {
Objects.requireNonNull(other);
if (other.value != null) {
final int length = other.value.length();
// lock the length so that we can seize the data to be appended
// before initiate copying to avoid interference, especially when
// merge 'this'
StringBuilder builder = prepareBuilder();
builder.append(other.value, other.prefix.length(), length);
}
return this;
}
//初始化,先添加前缀,有了之后每次先添加间隔符
private StringBuilder prepareBuilder() {
if (value != null) {
value.append(delimiter);
} else {
value = new StringBuilder().append(prefix);
}
return value;
}
public int length() {
// Remember that we never actually append the suffix unless we return
// the full (present) value or some sub-string or length of it, so that
// we can add on more if we need to.
//不忘添加后缀的长度
return (value != null ? value.length() + suffix.length() :
emptyValue.length());
}
}
方式六:StringUtils.join()
实战项目中,我们处理字符串的时候,经常会用到这个类.org.apache.commons.lang3.StringUtils包下,该方法更善于拼接数组中的字符串,并且不用担心 NullPointerException。
String aa = "今天";
String bb = "明天";
String ids[] = {"1","2","3"};
System.out.println(StringUtils.join(aa,bb,"-","124"));
String join1 = StringUtils.join(ids,",");
System.out.println(join1);
总结
到此这篇关于Java中字符串常见的一些拼接方式的文章就介绍到这了,更多相关Java字符串拼接方式内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!