String vs StringBuilder Methods
Method | String | StringBuilder | Description |
---|---|---|---|
charAt(int index) | ✓ | ✓ | Returns the character at the specified index. |
length() | ✓ | ✓ | Returns the length of the string. |
substring(int start) | ✓ | ✓ | Returns a new string starting from the specified index to the end. |
substring(int start, int end) | ✓ | ✓ | Returns a new string starting from start to end-1. |
indexOf(String str) | ✓ | ✓ | Returns the index of the first occurrence of the specified string. |
indexOf(String str, int fromIndex) | ✓ | ✓ | Returns the index of the specified string, starting from the given index. |
lastIndexOf(String str) | ✓ | ✓ | Returns the index of the last occurrence of the specified string. |
lastIndexOf(String str, int fromIndex) | ✓ | ✓ | Returns the last index of the specified string, searching backward. |
append(String str) | ✗ | ✓ | Appends the specified string to the current string. |
insert(int offset, String str) | ✗ | ✓ | Inserts the string at the specified offset. |
replace(CharSequence old, CharSequence new) | ✓ | ✓ | Replaces occurrences of a substring with a new substring. |
reverse() | ✗ | ✓ | Reverses the sequence of characters. |
toString() | ✓ | ✓ | Returns the string representation of the object. |
equals(Object obj) | ✓ | ✗ | Compares this string to the specified object. |
compareTo(String anotherString) | ✓ | ✗ | Compares two strings lexicographically. |
concat(String str) | ✓ | ✗ | Concatenates the specified string to the end of this string. |
contains(CharSequence s) | ✓ | ✗ | Returns true if this string contains the specified sequence. |
isEmpty() | ✓ | ✗ | Checks if the string is empty (length == 0). |
trim() | ✓ | ✗ | Removes leading and trailing whitespace. |
startsWith(String prefix) | ✓ | ✗ | Checks if the string starts with the specified prefix. |
endsWith(String suffix) | ✓ | ✗ | Checks if the string ends with the specified suffix. |
toLowerCase() | ✓ | ✗ | Converts all characters to lowercase. |
toUpperCase() | ✓ | ✗ | Converts all characters to uppercase. |
split(String regex) | ✓ | ✗ | Splits the string based on the given regular expression. |
replaceAll(String regex, String replacement) | ✓ | ✗ | Replaces all substrings matching a regex with a new string. |
delete(int start, int end) | ✗ | ✓ | Deletes characters from start to end. |
deleteCharAt(int index) | ✗ | ✓ | Deletes the character at the specified index. |
setCharAt(int index, char ch) | ✗ | ✓ | Sets the character at the specified index. |
ensureCapacity(int minimumCapacity) | ✗ | ✓ | Ensures that the capacity of the StringBuilder is at least minimumCapacity. |
Key Differences Between String and StringBuilder
- String: String is immutable, so it doesn't support methods like
append()
,insert()
,reverse()
,delete()
, orsetCharAt()
. - StringBuilder: StringBuilder is mutable and optimized for string modifications with methods like
append()
,insert()
,reverse()
,delete()
, andsetCharAt()
.
Both classes share methods for accessing characters (charAt()
), finding substrings (indexOf()
, lastIndexOf()
), and extracting substrings (substring()
), but StringBuilder is designed for in-place modifications.