简体   繁体   中英

StringBuilder.append alternative

Hi can someone suggest to me how I can refactor this code from using x.append and use only appened ( removing the x. every time). Thanks

StringBuilder x = new StringBuilder();
x.append("\n.O/");
x.append(fields.nextToken()); 
x.append("/");
x.append(fields.nextToken()); // Date
x.append("/");
x.append(fields.nextToken());
x.append("/Y");

It sounds like the specific code you're looking for is just

StringBuilder x = new StringBuilder();
x.append("\n.O/")
  .append(fields.nextToken())
  .append("/")
  .append(fields.nextToken()) // Date
  .append("/")
  .append(fields.nextToken())
  .append("/Y");

There are several other -- arguably better -- approaches to writing this code, but you specifically asked for chaining the append statements.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM