-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStringBuilderExploration
63 lines (52 loc) · 1.91 KB
/
StringBuilderExploration
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
public class Stringbuilder {
public static void main(String[] args) {
//Inefficient Method
String name="elizabeth";
//Changes first character (e) to E, and attaches 'lizabeth'
name=Character.toUpperCase(name.charAt(0))+name.substring(1);
System.out.println(name);
//String Builders Method
StringBuilder nameTwo=new StringBuilder ("elizabeth");
//Index of value (0) and then method applied (character.toUpperCase
nameTwo.setCharAt(0, Character.toUpperCase(nameTwo.charAt(0)));
System.out.println(nameTwo);
//Normal method adding to string
String study="Software";
study+="Engineering";
System.out.println(study);
//String Builders
StringBuilder studyTwo= new StringBuilder ("Software");
//Append is more efficient than +=
studyTwo.append("Engineering");
System.out.println(studyTwo);
//StringBuilder strBuild = new StringBuilder("Comic");
//strBuild.setCharAt(5, 's')
//Capacity of the strBuild=5+(default of 16)
//Command above throws out of bounds, to add, you must append
StringBuilder newWord= new StringBuilder ("Comil");
newWord.setCharAt(4,'c');
newWord.append('s');
System.out.println(newWord);
//Delete Method
StringBuilder sequence = new StringBuilder("ABCDEFG");
sequence.deleteCharAt(3);
//Deletes 'D'
System.out.println(sequence);
//Multiple delete
StringBuilder sequenceTwo = new StringBuilder("ABCDEFG");
//Beginning is inclusive, end is exclusive
sequenceTwo.delete(1, 3);
System.out.println(sequenceTwo);
//Inserting Words
StringBuilder concentration= new StringBuilder ("Science");
concentration.insert(0, "Computer ");
System.out.println(concentration);
//Checking Capacity
StringBuilder strBuild = new StringBuilder();
//Default is 16
System.out.println(strBuild.capacity());
strBuild.ensureCapacity(20);
//Needs a min of 20, so new capacity= 2(old capacity+1)
System.out.println(strBuild.capacity());
}
}