Eclipse: Convert String Concat to StringBuilder

Here is a simple yet effective trick for all Eclipse users. In Java we do lot of String concatenation using plain old “” + “”. It is easy but not too effective. StringBuilder is always preferred if you have too many strings to concate.


In below screencast, I convert concatenated strings to StringBuilder using eclipse Ctrl + 1 option.

eclipse-string-concatenate-stringbuilder


Move cursor on String “foo” and press Ctrl + 1. It opens a context menu. Select option Use “StringBuilder” for string concatenation



String foo = "This" + "is" + "Sparta";

System.out.println(foo);

Once done, the code will be converted to below:



StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("This");
stringBuilder.append("is");
stringBuilder.append("Sparta");
String foo = stringBuilder.toString();

System.out.println(foo);

Enjoy :)








via ViralPatel.net http://feedproxy.google.com/~r/viralpatelnet/~3/31caPuoFL5s/

No comments:

Post a Comment

If you have any question please let me know

Windows 11 Insider Preview Build 29558.1000 Released to New Canary Channel

UPDATE: Windows 11 Insider Preview build 29558.1000 released to the new Canary channel. Windows Insiders on the refreshed Canary Channel wil...