A StringBuilder allows strings to built up.
There are 4 constructors and the one without any arguments sets initial size at 16. As a StringBuilder aquires more characters, the capacity is automatically increased.
// *** 1. Start (Constructors) System.out.println("*** Starting 1... ***"); StringBuilder sb1 = new StringBuilder(); StringBuilder sb2 = new StringBuilder(32); String s1 = "This"; StringBuilder sb3 = new StringBuilder(s1); StringBuilder sb4 = new StringBuilder("THIS "); // *** 1. End
We can find the capacity of a StringBuilder using the capacity() method. The append() method allows for characters to be added to end of the StringBuilder.
// *** 2. Start (capacity, append) System.out.println("*** Starting 2... ***"); System.out.println("sb1 capacity: " + sb1.capacity()); System.out.println("sb2 capacity: " + sb2.capacity()); System.out.println("sb3 capacity: " + sb3.capacity()); System.out.println("sb4 capacity: " + sb4.capacity()); sb1.append(" Hello "); sb2.append(" Hello ").append(" World "); sb3.append(false); sb4.append(true).append(sb3); System.out.println("sb1 = " + sb1); System.out.println("sb2 = " + sb2); System.out.println("sb3 = " + sb3); System.out.println("sb4 = " + sb4); // *** 2. End
insert() method can add a string to StringBuilder at any index. The length() method gives current length of StringBuilder.
// *** 3. Start (insert,length) System.out.println("*** Starting 3... ***"); sb1.insert(0, "_Starting at 0_"); sb2.insert(1, "_Starting at 1_"); sb3.insert(2, "_Starting at 2_"); sb4.insert(3, "_Starting at 3_"); System.out.format("sb1 = %s\t(L=%d)\n",sb1,sb1.length()); System.out.format("sb2 = %s\t(L=%d)\n",sb2,sb2.length()); System.out.format("sb3 = %s\t(L=%d)\n",sb3,sb3.length()); System.out.format("sb4 = %s\t(L=%d)\n",sb4,sb4.length()); // *** 3. End
replace() method can be used to replace a substring with a new substring. Finally, after modifications, we can create a String object from a StringBuilder object.
// *** 4. Start (replace,toString) System.out.println("*** Starting 4... ***"); sb1.replace(0, 0, "-java-"); String str1 = sb1.toString(); System.out.format("str1 = %s\t(L=%d)\n",str1,str1.length()); sb2.replace(0, 6, "-java-"); String str2 = sb2.toString(); System.out.format("str2 = %s\t(L=%d)\n",str2,str2.length()); sb3.replace(0, 2, "-java-"); String str3 = sb3.toString(); System.out.format("str3 = %s\t(L=%d)\n",str3,str3.length()); sb4.replace(0, 3, "-java-"); String str4 = sb4.toString(); System.out.format("str4 = %s\t(L=%d)\n",str4,str4.length()); // *** 4. End
package com.javaAndroid.ex6; public class Ex6 { public static void main(String[] args) { // *** 1. Start (Constructors) System.out.println("*** Starting 1... ***"); StringBuilder sb1 = new StringBuilder(); StringBuilder sb2 = new StringBuilder(32); String s1 = "This"; StringBuilder sb3 = new StringBuilder(s1); StringBuilder sb4 = new StringBuilder("THIS "); // *** 1. End // *** 2. Start (capacity, append) System.out.println("*** Starting 2... ***"); System.out.println("sb1 capacity: " + sb1.capacity()); System.out.println("sb2 capacity: " + sb2.capacity()); System.out.println("sb3 capacity: " + sb3.capacity()); System.out.println("sb4 capacity: " + sb4.capacity()); sb1.append(" Hello "); sb2.append(" Hello ").append(" World "); sb3.append(false); sb4.append(true).append(sb3); System.out.println("sb1 = " + sb1); System.out.println("sb2 = " + sb2); System.out.println("sb3 = " + sb3); System.out.println("sb4 = " + sb4); // *** 2. End // *** 3. Start (insert,length) System.out.println("*** Starting 3... ***"); sb1.insert(0, "_Starting at 0_"); sb2.insert(1, "_Starting at 1_"); sb3.insert(2, "_Starting at 2_"); sb4.insert(3, "_Starting at 3_"); System.out.format("sb1 = %s\t(L=%d)\n",sb1,sb1.length()); System.out.format("sb2 = %s\t(L=%d)\n",sb2,sb2.length()); System.out.format("sb3 = %s\t(L=%d)\n",sb3,sb3.length()); System.out.format("sb4 = %s\t(L=%d)\n",sb4,sb4.length()); // *** 3. End // *** 4. Start (replace,toString) System.out.println("*** Starting 4... ***"); sb1.replace(0, 0, "-java-"); String str1 = sb1.toString(); System.out.format("str1 = %s\t(L=%d)\n",str1,str1.length()); sb2.replace(0, 6, "-java-"); String str2 = sb2.toString(); System.out.format("str2 = %s\t(L=%d)\n",str2,str2.length()); sb3.replace(0, 2, "-java-"); String str3 = sb3.toString(); System.out.format("str3 = %s\t(L=%d)\n",str3,str3.length()); sb4.replace(0, 3, "-java-"); String str4 = sb4.toString(); System.out.format("str4 = %s\t(L=%d)\n",str4,str4.length()); // *** 4. End } }
No comments:
Post a Comment