Skip to content

Commit 384858c

Browse files
committed
Add string modification to TestString.java
1 parent aeb77fc commit 384858c

1 file changed

Lines changed: 6 additions & 2 deletions

File tree

TestString.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,17 @@ private static void stringBuilderDemo() {
4747
}
4848
String result = sb2.toString();
4949
System.out.println(result);
50+
51+
sb2.setCharAt(2, 'l');
52+
sb2.setCharAt(7, 'j');
53+
System.out.println(sb2);
5054
}
5155

5256
// StringBuilder is essentially a wrapper of char[],
5357
// with some additional funtionalities including resizing the char array.
5458
// If char array can do the job then we should just use char array
5559
private static void modifyString(String str) {
56-
char[] chars = str.toCharArray();
60+
char[] chars = str.toCharArray(); //Returns a deep copy of the char array in the string
5761
for (char c : chars) {
5862
System.out.print(c);
5963
}
@@ -63,7 +67,7 @@ private static void modifyString(String str) {
6367
chars[i] = '#';
6468
}
6569
}
66-
String modified = String.valueOf(chars);
70+
String modified = String.valueOf(chars); //Create a new string with a deep copy of chars array
6771
System.out.println("Modified: " + modified);
6872
}
6973

0 commit comments

Comments
 (0)