String Methods

String is a fundamental class in Java for working with text. It provides a wide range of methods for manipulating and inspecting strings. This documentation covers some of the most frequently used methods with real-time examples.

1. length()

Method Signature

int length()

Description

This method returns the length (the number of characters) of the String.

Example

String text = "Hello, World!";
int length = text.length(); // length will be 13

2. charAt(int index)

Method Signature

char charAt(int index)

Description

This method returns the character at the specified index in the String.

Example

String text = "Java";
char character = text.charAt(1); // character will be 'a'

3. substring(int beginIndex)

Method Signature

String substring(int beginIndex)

Description

This method returns a new String that is a substring of the original string starting from the specified beginIndex.

Example

String text = "Hello, World!";
String substring = text.substring(7); // substring will be "World!"

4. substring(int beginIndex, int endIndex)

Method Signature

String substring(int beginIndex, int endIndex)

Description

This method returns a new String that is a substring of the original string, starting from beginIndex (inclusive) and ending at endIndex (exclusive).

Example

String text = "Hello, World!";
String substring = text.substring(7, 12); // substring will be "World"

5. concat(String str)

Method Signature

String concat(String str)

Description

This method concatenates the specified string (str) to the end of the original string and returns the resulting String.

Example

String text = "Hello";
String newText = text.concat(", World!"); // newText will be "Hello, World!"

6. equals(Object obj)

Method Signature

boolean equals(Object obj)

Description

This method compares the content of two strings and returns true if they are equal, false otherwise.

Example

String str1 = "Hello";
String str2 = "Hello";
boolean isEqual = str1.equals(str2); // isEqual will be true

7. equalsIgnoreCase(String anotherString)

Method Signature

boolean equalsIgnoreCase(String anotherString)

Description

This method compares two strings, ignoring their case (uppercase/lowercase), and returns true if they are equal, false otherwise.

Example

String str1 = "Hello";
String str2 = "hello";
boolean isEqual = str1.equalsIgnoreCase(str2); // isEqual will be true

8. startsWith(String prefix)

Method Signature

boolean startsWith(String prefix)

Description

This method checks if the String starts with the specified prefix and returns true if it does, false otherwise.

Example

String text = "Hello, World!";
boolean startsWithHello = text.startsWith("Hello"); // startsWithHello will be true

9. endsWith(String suffix)

Method Signature

boolean endsWith(String suffix)

Description

This method checks if the String ends with the specified suffix and returns true if it does, false otherwise.

Example

String text = "Hello, World!";
boolean endsWithWorld = text.endsWith("World!"); // endsWithWorld will be true

10. contains(CharSequence sequence)

Method Signature

boolean contains(CharSequence sequence)

Description

This method checks if the String contains the specified sequence of characters and returns true if it does, false otherwise.

Example

String text = "The quick brown fox";
boolean containsFox = text.contains("fox"); // containsFox will be true

11. replace(CharSequence target, CharSequence replacement)

Method Signature

String replace(CharSequence target, CharSequence replacement)

Description

This method replaces all occurrences of the target character sequence with the replacement character sequence and returns the resulting String.

Example

String text = "I love Java. Java is great!";
String replacedText = text.replace("Java", "Python");
// replacedText will be "I love Python. Python is great!"

12. trim()

Method Signature

String trim()

Description

This method removes leading and trailing whitespace from the String and returns the resulting String.

Example

String text = "   This is a string with whitespace.   ";
String trimmedText = text.trim();
// trimmedText will be "This is a string with whitespace."

13. split(String regex)

Method Signature

String[] split(String regex)

Description

This method splits the String into an array of substrings based on the provided regular expression (regex) and returns the array.

Example

String text = "apple,banana,cherry";
String[] fruits = text.split(",");
// fruits will be ["apple", "banana", "cherry"]

14. toUpperCase() and toLowerCase()

Method Signature

String toUpperCase()
String toLowerCase()

Description

These methods convert all characters in the String to uppercase or lowercase, respectively, and return the resulting String.

Example

String text = "Hello, World!";
String upperCaseText = text.toUpperCase(); // upperCaseText will be "HELLO, WORLD!"
String lowerCaseText = text.toLowerCase(); // lowerCaseText will be "hello, world!"

15. isEmpty()

Method Signature

boolean isEmpty()

Description

This method checks if the String is empty (has a length of 0) and returns true if it is, false otherwise.

Example

String emptyString = "";
boolean isEmpty = emptyString.isEmpty(); // isEmpty will be true

These are some of the most frequently used String methods in Java, along with real-time examples. Strings are essential in Java for working with text data, and understanding these methods will help you manipulate and analyze text efficiently in your Java applications.

Last updated