Home >Java >javaTutorial >Convert string to StringBuilder in Java

Convert string to StringBuilder in Java

王林
王林forward
2023-09-02 15:57:061408browse

The

append()Convert string to StringBuilder in Java method of the

StringBuilder class accepts a String value and adds it to the current object.

Convert the string value to a StringBuilder object -
  • Get the string value.

  • Append using the append() method to get the string to the StringBuilder.

Example

In the following Java program, we convert an array of strings into a single StringBuilder object.

Real-time demonstration

public class StringToStringBuilder {
   public static void main(String args[]) {
      String strs[] = {"Arshad", "Althamas", "Johar", "Javed", "Raju", "Krishna" };
      StringBuilder sb = new StringBuilder();
      sb.append(strs[0]);
      sb.append(" "+strs[1]);
      sb.append(" "+strs[2]);
      sb.append(" "+strs[3]);
      sb.append(" "+strs[4]);
      sb.append(" "+strs[5]);
      System.out.println(sb.toString());
   }
}

Output

Arshad Althamas Johar Javed Raju Krishna

The above is the detailed content of Convert string to StringBuilder in Java. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete