其他
惊,Java 字符串拼接竟然有这么多玩法!| CSDN 原力计划
String chenmo = "沉默";
String wanger = "王二";
System.out.println(chenmo + wanger);
String chenmo = "\u6C89\u9ED8"; // 沉默
String wanger = "\u738B\u4E8C"; // 王二
System.out.println((new StringBuilder(String.valueOf(chenmo))).append(wanger).toString());
public StringBuilder append(String str) {
super.append(str);
return this;
}
public AbstractStringBuilder append(String str) {
if (str == null)
return appendNull();
int len = str.length();
ensureCapacityInternal(count + len);
str.getChars(0, len, value, count);
count += len;
return this;
}
private AbstractStringBuilder appendNull() {
int c = count;
ensureCapacityInternal(c + 4);
final char[] value = this.value;
value[c++] = 'n';
value[c++] = 'u';
value[c++] = 'l';
value[c++] = 'l';
count = c;
return this;
}
private void ensureCapacityInternal(int minimumCapacity) {
// overflow-conscious code
if (minimumCapacity - value.length > 0) {
value = Arrays.copyOf(value,
newCapacity(minimumCapacity));
}
}
str.getChars(0, len, value, count)
public synchronized StringBuffer append(String str) {
toStringCache = null;
super.append(str);
return this;
}
String chenmo = "沉默";
String wanger = "王二";
System.out.println(chenmo.concat(wanger));
chenmo += wanger
chenmo = chenmo.concat(wanger)
public String concat(String str) {
int otherLen = str.length();
if (otherLen == 0) {
return this;
}
int len = value.length;
char buf[] = Arrays.copyOf(value, len + otherLen);
str.getChars(buf, len);
return new String(buf, true);
}
if (otherLen == 0) {
return this;
}
char buf[] = Arrays.copyOf(value, len + otherLen);
str.getChars(buf, len);
return new String(buf, true);
${chenmo.concat('-').concat(wanger)}
String chenmo = "沉默";
String wanger = "王二";
String cmower = String.join("", chenmo, wanger);
System.out.println(cmower);
String message = String.join("-", "王二", "太特么", "有趣了");
public static String join(CharSequence delimiter, CharSequence... elements) {
Objects.requireNonNull(delimiter);
Objects.requireNonNull(elements);
// Number of elements not likely worth Arrays.stream overhead.
StringJoiner joiner = new StringJoiner(delimiter);
for (CharSequence cs: elements) {
joiner.add(cs);
}
return joiner.toString();
}
String chenmo = "沉默";
String wanger = "王二";
StringUtils.join(chenmo, wanger);
StringUtils.join(null) = null
StringUtils.join([]) = ""
StringUtils.join([null]) = ""
StringUtils.join(["a", "b", "c"]) = "abc"
StringUtils.join([null, "", "a"]) = "a"
public static String join(final Object[] array, String separator, final int startIndex, final int endIndex) {
if (array == null) {
return null;
}
if (separator == null) {
separator = EMPTY;
}
final StringBuilder buf = new StringBuilder(noOfItems * 16);
for (int i = startIndex; i < endIndex; i++) {
if (i > startIndex) {
buf.append(separator);
}
if (array[i] != null) {
buf.append(array[i]);
}
}
return buf.toString();
}
String result = "";
for (int i = 0; i < 100000; i++) {
result += "六六六";
}
StringBuilder sb = new StringBuilder();
for (int i = 0; i < 100000; i++) {
sb.append("六六六");
}
热 文 推 荐
☞讯飞轮值总裁胡郁:大数据是人工智能产业落地的必要保障| BDTC 2019