Refactor String concatenation to StringBuilder with intelliJ

倾然丶 夕夏残阳落幕 提交于 2020-01-04 05:36:35

问题


I was designated to make refactoring on a project, and I came across this situation

 this.path = DESTINY + deploy.name() + FILE_SEPARATOR + delivery.getSystem().getCode()
      + FILE_SEPARATOR + delivery.getName() + FILE_SEPARATOR + delivery.getEnviroment().getName();

I want to refactor it to something like this:

StringBuilder tmpPath = new StringBuilder();
tmpPath.append(DESTINY);
tmpPath.append(deploy.name());
tmpPath.append(FILE_SEPARATOR);
tmpPath.append(delivery.getSystem().getCode());
tmpPath.append(FILE_SEPARATOR);
tmpPath.append(delivery.getName());
tmpPath.append(FILE_SEPARATOR);
tmpPath.append(delivery.getEnviroment().getName());
this.path = tmpPath.toString();

Or

StringBuilder path = new StringBuilder();

...

this.path.append(DESTINY);
this.path.append(deploy.name());
this.path.append(FILE_SEPARATOR);
this.path.append(delivery.getSystem().getCode());
this.path.append(FILE_SEPARATOR);
this.path.append(delivery.getName());
this.path.append(FILE_SEPARATOR);
this.path.append(delivery.getEnviroment().getName());

Whats is the fastest way to do that ?


回答1:


Just hit Alt + Enter when having cursor on your original code:

Then you can choose the variant you prefer the most such as replacing it with String Builder. IDEA will do all the work for you.



来源:https://stackoverflow.com/questions/48543138/refactor-string-concatenation-to-stringbuilder-with-intellij

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!