0% found this document useful (0 votes)
3 views2 pages

String Functions

In Java, a string is an immutable sequence of characters, represented as objects of the String class. Key characteristics include immutability, a string pool for memory optimization, and UTF-16 character encoding. Common string methods include length, character access, substring extraction, index searching, comparison, case conversion, trimming, replacement, splitting, and joining.

Uploaded by

forcoc743
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views2 pages

String Functions

In Java, a string is an immutable sequence of characters, represented as objects of the String class. Key characteristics include immutability, a string pool for memory optimization, and UTF-16 character encoding. Common string methods include length, character access, substring extraction, index searching, comparison, case conversion, trimming, replacement, splitting, and joining.

Uploaded by

forcoc743
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Strings in Java

Definition: In Java, a string is a sequence of characters. Strings are objects of


the String class, which is part of the java.lang package. Strings are immutable,
meaning that once a string object is created, its value cannot be changed. Any
operation that seems to modify a string actually creates a new string.

Key Characteristics of Strings


1. Immutability:
o Strings cannot be changed after they are created. Any modification results in the
creation of a new string object.
2. String Pool:
o Java maintains a pool of strings to optimize memory usage. When a string is created
using string literals, Java checks the pool to see if an identical string already exists. If
it does, the reference to the existing string is returned instead of creating a new
object.
3. Character Encoding:
o Strings in Java are encoded in UTF-16, allowing for a wide range of characters from
different languages.

Common String Functions (Methods)


1. Length:
o int length() : Returns the number of characters in the string.
2. Character Access:
o char charAt(int index) : Returns the character at the specified index.
3. Substring:
o String substring(int beginIndex) : Returns a substring starting from the
specified index to the end of the string.
o String substring(int beginIndex, int endIndex) : Returns a substring from the
specified beginIndex to endIndex - 1 .
4. Index of:
o int indexOf(String str) : Returns the index of the first occurrence of the specified
substring.
o int lastIndexOf(String str) : Returns the index of the last occurrence of the
specified substring.
5. Comparison:
o boolean equals(Object obj) : Compares the string to the specified object for
equality.
o boolean equalsIgnoreCase(String anotherString) : Compares the string to
another string, ignoring case considerations.
6. Case Conversion:
o String toLowerCase() : Converts all characters in the string to lowercase.
o String toUpperCase() : Converts all characters in the string to uppercase.
7. Trimming:
o String trim() : Removes leading and trailing whitespace from the string.
8. Replacement:
o String replace(char oldChar, char newChar) : Replaces all occurrences of a
specified character with a new character.
o String replaceAll(String regex, String replacement) : Replaces each
substring of the string that matches the given regular expression with the specified
replacement.
9. Splitting:
o String[] split(String regex) : Splits the string around matches of the given
regular expression and returns an array of substrings.
10. Joining:
o static String join(CharSequence delimiter, CharSequence... elements) :
Joins the given elements into a single string with the specified delimiter.

You might also like