In Java, a string is an object representing a sequence of characters. It’s used to store and manipulate text-based information.
Properties of Strings:
- Immutable Nature: Once a string is created, its content cannot be changed. Any operation that appears to modify a string actually creates a new string.
- Creation: Strings can be created using double quotes with string literals (
"Hello, Java!"), using theStringclass constructor (String str = new String("Hello, Java!");), or through implicit string literals (String str = "Hello, Java!";). - Common Operations:
- Concatenation: Joining strings together.
- Substring: Extracting parts of a string.
- Length: Finding the number of characters in a string.
- Searching and Manipulation: Finding characters or sequences, replacing parts, etc.
- String Pool: Java maintains a “string pool” for optimization. Literal strings are pooled, so if the same string is created multiple times, Java reuses the existing instance.
- Handling Textual Data: Strings are extensively used for managing and manipulating textual information in Java programs.
Common Methods of Strings:
Length: length();
Returns the length (number of characters) of the string.
Concatenation: concat(String str);
Concatenates the specified string at the end of the current string and returns a new string.
Substring: substring(int beginIndex);
Returns a new string that is a substring of the original string, starting from beginIndex to the end.
substring(int beginIndex, int endIndex);
Returns a substring from beginIndex to endIndex – 1.
Character Extraction: charAt(int index);
Returns the character at the specified index.
Searching: indexOf(String str);
Returns the index of the first occurrence of the specified substring.
lastIndexOf(String str);
Returns the index of the last occurrence of the specified substring.
Checking Existence: contains(CharSequence s);
Check if the string contains the specified sequence of characters.
Comparison: equals(Object anObject);
Compares two strings for equality.
equalsIgnoreCase(String anotherString);
Compares two strings, ignoring case considerations.
Case Conversion: toUpperCase(), toLowerCase()
Converts the string to uppercase or lowercase.
Trimming: trim();
Removes leading and trailing white spaces from the string.
Replacing: replace(char oldChar, char newChar);
Replace all occurrences of oldChar in the string with newChar.
Splitting: split(String regex);
Splits the string based on the given regular expression and returns an array of strings.
Conversion: valueOf();
Converts different data types to strings.
In Java, while the String class is immutable and widely used, there are alternative classes that offer different features or functionalities that might suit specific needs better:
StringBuffer:
Mutability: Unlike String, StringBuffer allows you to change the content of the string it holds.
Appending and Modifying: It offers efficient methods for appending, inserting, deleting, and modifying strings.
Thread-Safety: It’s synchronized, ensuring safe usage in multi-threaded environments where multiple threads might access the same string concurrently.
StringBuilder:
Mutability: Similar to StringBuffer, StringBuilder allows modification of the content it holds.
Performance: It’s faster in a single-threaded environment compared to StringBuffer due to lack of synchronization, making it ideal for scenarios where thread safety isn’t a concern.
Comparison with String:
Mutability: Both StringBuffer and StringBuilder allow modification of strings, unlike regular String objects that are immutable (unchangeable).
Efficiency in Concatenation and Modifications: They provide more efficient ways to concatenate, modify, or build strings compared to repeatedly creating new String objects, especially when dealing with extensive string manipulations.
Check the Official Doc: Class String