例如,
String x= "ABC";
将 ABC 转换为“ABC”的最佳方法是什么?
最佳答案
与 +
相比,这将创建更少的中间 String
对象
public static String quote(String s) {
return new StringBuilder()
.append('\'')
.append(s)
.append('\'')
.toString();
}
public static void main(String[] args) {
String x = "ABC";
String quotedX = quote(x);
System.out.println(quotedX);
}
打印'ABC'
关于java - 这是在 java 中为字符串 append 单引号的最佳方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29516260/