使用intelliJ将字符串连接重构为StringBuilder [英] Refactor String concatenation to StringBuilder with intelliJ

查看:154
本文介绍了使用intelliJ将字符串连接重构为StringBuilder的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我被指定对一个项目进行重构,而我遇到了这种情况

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();

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());

什么是最快的方法?

推荐答案

将光标放在原始代码上时,只需按 Alt + Enter 即可.

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

然后,您可以选择最喜欢的变体,例如将其替换为String Builder. IDEA将为您完成所有工作.

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.

这篇关于使用intelliJ将字符串连接重构为StringBuilder的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆