Java String vs StringBuilder
In Java, String is immutable (cannot be changed once created), whereas StringBuilder is mutable
(can be modified directly). Below are the commonly used methods, syntax, and examples for both.
1. String (Immutable)
length() - Returns the length of the string.
String s = "hello";
System.out.println(s.length()); // 5
charAt(int index) - Returns the character at a given index.
String s = "hello";
System.out.println(s.charAt(1)); // e
substring(int start, int end) - Extracts a substring.
String s = "hello";
System.out.println(s.substring(1,4)); // ell
equals(String another) - Checks content equality.
String s1 = "java"; String s2 = "java";
System.out.println(s1.equals(s2)); // true
equalsIgnoreCase(String another) - Case-insensitive comparison.
String s1 = "Java"; String s2 = "java";
System.out.println(s1.equalsIgnoreCase(s2)); // true
compareTo(String another) - Lexicographic comparison.
String s1 = "apple"; String s2 = "banana";
System.out.println(s1.compareTo(s2)); // -1
toLowerCase() - Converts string to lowercase.
String s = "JAVA";
System.out.println(s.toLowerCase()); // java
toUpperCase() - Converts string to uppercase.
String s = "java";
System.out.println(s.toUpperCase()); // JAVA
trim() - Removes leading and trailing spaces.
String s = " hello ";
System.out.println(s.trim()); // "hello"
replace(char old, char new) - Replaces occurrences of a char.
String s = "java";
System.out.println(s.replace("a","o")); // jovo
contains(CharSequence cs) - Checks if substring exists.
String s = "hello";
System.out.println(s.contains("he")); // true
startsWith(String prefix) - Checks if string starts with prefix.
String s = "hello";
System.out.println(s.startsWith("he")); // true
endsWith(String suffix) - Checks if string ends with suffix.
String s = "hello";
System.out.println(s.endsWith("lo")); // true
split(String regex) - Splits string into array using regex.
String s = "a,b,c";
String[] arr = s.split(","); // [a, b, c]
2. StringBuilder (Mutable)
append(String str) - Appends string at the end.
StringBuilder sb = new StringBuilder("Hello");
sb.append(" World");
System.out.println(sb); // Hello World
insert(int offset, String str) - Inserts string at given position.
StringBuilder sb = new StringBuilder("Hello");
sb.insert(1,"i");
System.out.println(sb); // Hiello
replace(int start, int end, String str) - Replaces part of string.
StringBuilder sb = new StringBuilder("Hello");
sb.replace(1,3,"i");
System.out.println(sb); // Hilo
delete(int start, int end) - Deletes characters between indexes.
StringBuilder sb = new StringBuilder("Hello");
sb.delete(1,3);
System.out.println(sb); // Hlo
deleteCharAt(int index) - Deletes char at given index.
StringBuilder sb = new StringBuilder("Hello");
sb.deleteCharAt(1);
System.out.println(sb); // Hllo
reverse() - Reverses the string.
StringBuilder sb = new StringBuilder("abc");
sb.reverse();
System.out.println(sb); // cba
capacity() - Returns current capacity.
StringBuilder sb = new StringBuilder();
System.out.println(sb.capacity());
ensureCapacity(int min) - Ensures minimum capacity.
StringBuilder sb = new StringBuilder();
sb.ensureCapacity(50);
setLength(int newLength) - Sets length (truncates or pads).
StringBuilder sb = new StringBuilder("hello");
sb.setLength(2);
System.out.println(sb); // he
charAt(int index) - Returns char at index.
StringBuilder sb = new StringBuilder("hello");
System.out.println(sb.charAt(1)); // e
setCharAt(int index, char ch) - Changes character at index.
StringBuilder sb = new StringBuilder("hello");
sb.setCharAt(1, "a");
System.out.println(sb); // hallo
substring(int start, int end) - Returns substring (like String).
StringBuilder sb = new StringBuilder("hello");
System.out.println(sb.substring(1,4)); // ell
length() - Returns length of sequence.
StringBuilder sb = new StringBuilder("hello");
System.out.println(sb.length()); // 5