Java StringBuilder ClassJava StringBuilder class is used to create mutable (modifiable) String. The Java StringBuilder class is same as StringBuffer class except that it is non-synchronized. It is available since JDK 1.5. Important Constructors of StringBuilder class
Important methods of StringBuilder class
Java StringBuilder ExamplesLet's see the examples of different methods of StringBuilder class. 1) StringBuilder append() methodThe StringBuilder append() method concatenates the given argument with this String. StringBuilderExample.java Output: Hello Java 2) StringBuilder insert() methodThe StringBuilder insert() method inserts the given string with this string at the given position. StringBuilderExample2.java Output: HJavaello 3) StringBuilder replace() methodThe StringBuilder replace() method replaces the given string from the specified beginIndex and endIndex. StringBuilderExample3.java Output: HJavalo 4) StringBuilder delete() methodThe delete() method of StringBuilder class deletes the string from the specified beginIndex to endIndex. StringBuilderExample4.java Output: Hlo 5) StringBuilder reverse() methodThe reverse() method of StringBuilder class reverses the current string. StringBuilderExample5.java Output: olleH 6) StringBuilder capacity() methodThe capacity() method of StringBuilder class returns the current capacity of the Builder. The default capacity of the Builder is 16. If the number of character increases from its current capacity, it increases the capacity by (oldcapacity*2)+2. For example if your current capacity is 16, it will be (16*2)+2=34. StringBuilderExample6.java Output: 16 16 34 7) StringBuilder ensureCapacity() methodThe ensureCapacity() method of StringBuilder class ensures that the given capacity is the minimum to the current capacity. If it is greater than the current capacity, it increases the capacity by (oldcapacity*2)+2. For example if your current capacity is 16, it will be (16*2)+2=34. StringBuilderExample7.java Output: 16 16 34 34 70 |
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India