Forum Stats

  • 3,837,072 Users
  • 2,262,223 Discussions
  • 7,900,198 Comments

Discussions

String Format Appending in Java

User_SQDZD
User_SQDZD Member Posts: 4 Green Ribbon

I wrote the following code in Java, and the getter methods return Strings:

String stringbuilder="";
stringbuilder = String.format("%d/%d\n", this.getSlotNum(), this.getPortNum());
stringbuilder = stringbuilder+String.format(" " + this.getPortSpeed() +"\n");
stringbuilder = stringbuilder+String.format("%d\n", this.getPortMode());

C# offers a StringBuilder class with an AppendFormat() function that allows me to attach a formatted string straight to the StringBuilder instance without having to concatenate it first. In the Java StringBuilder class, I find an AppendFormat() function.

After looking at this article from Scaler. It suggested the use of the isEmpty() method to check whether the StringBuilder object contains any sequence or not.

StringBuilder ob=new StringBuilder("");
   System.out.println(ob.isEmpty());
   ob.append("Aayush");
   System.out.println(ob.isEmpty());

Is there a better method to do what I'm attempting? Please, suggest!