StringBuilder格式化程序

时间:2012-12-03 10:59:58

标签: java

StringBuilder.append对于大型SQL语句看起来很难看,格式化要追加需要花费很多时间才能在Java中格式化StringBuilder / StringBuffer

1 个答案:

答案 0 :(得分:2)

String.format可能有助于提高代码的可读性:

String sql = String.format("select * from %s where %s = ?;", table, field);

StringBuilder替代方案:

String sql = new StringBuilder("select * from ")
                  .append(table)
                  .append(" where ")
                  .append(field)
                  .append(" = ")
                  .append("?")
                  .toString();