Java String Methods: Comprehensive Guide

In this comprehensive guide, we will explore some of the most important Java String methods.

We will provide detailed descriptions of each method, along with sample Java code and corresponding output to illustrate their usage. By the end of this article, you will have a solid understanding of these methods and be equipped to effectively work with strings in your Java projects.

All Java String Methods: Overview

Here’s an overview of various Java String methods in a tabular form:

MethodDescription
equals(Object obj)Compares this string to the specified object.
equalsIgnoreCase(String str)Compares this string to another string, ignoring case considerations.
isEmpty()Returns true if the string is empty (contains no characters).
startsWith(String prefix)Checks if the string starts with the specified prefix.
endsWith(String suffix)Checks if the string ends with the specified suffix.
contains(CharSequence sequence)Checks if the string contains the specified sequence of characters.
replace(char oldChar, char newChar)Replaces all occurrences of the specified character with a new character.
trim()Removes leading and trailing whitespace from the string.
replaceAll(String regex, String replacement)Replaces all occurrences of a specified regex pattern with a new string.
valueOf(Object obj)Converts the specified object to its string representation.
length()Returns the length of the string.
charAt(int index)Returns the character at the specified index.
substring(int beginIndex)Returns a substring starting from the specified index.
substring(int beginIndex, int endIndex)Returns a substring within the specified range.
concat(String str)Concatenates the specified string to the end of the current string.
indexOf(String str)Returns the index of the first occurrence of the specified string.
toUpperCase()Converts the string to uppercase.
toLowerCase()Converts the string to lowercase.

Commonly Used Java String Methods

Java String Methods

length()

The length() method returns the number of characters in a string. It is a simple yet crucial method for determining the length of a string. Here’s an example:

String text = "Hello, Java!";
int length = text.length();
System.out.println("Length of the string: " + length);
Length of the string: 13

charAt(int index)

The charAt() method returns the character at the specified index position in a string. It allows you to access individual characters within a string. Here’s an example:

String text = "Hello, Java!";
char character = text.charAt(7);
System.out.println("Character at index 7: " + character);
Character at index 7: J

substring(int beginIndex)

The substring() method returns a new string that is a substring of the original string, starting from the specified index. It allows you to extract a portion of a string. Here’s an example:

String text = "Hello, Java!";
String substring = text.substring(7);
System.out.println("Substring from index 7: " + substring);
Substring from index 7: Java!

substring(int startIndex, int endIndex)

This method allows you to extract a portion of a string based on the specified start and end index.

String text = "Hello, Java!";
String substring = text.substring(7, 11);
System.out.println("Substring from index 7 to 10: " + substring);
Substring from index 7 to 10: Java

indexOf(String str)

The indexOf() method returns the index of the first occurrence of the specified substring within a string. It helps in locating the position of a substring. Here’s an example:

String text = "Hello, Java!";
int index = text.indexOf("Java");
System.out.println("Index of 'Java': " + index);
Index of 'Java': 7

concat(String str)

The concat() method concatenates the specified string to the end of the current string. It allows you to combine strings together. Here’s an example:

String greeting = "Hello";
String name = "Java";
String message = greeting.concat(", " + name + "!");
System.out.println("Concatenated message: " + message);
Concatenated message: Hello, Java!

toUpperCase() and toLowerCase()

The toUpperCase() and toLowerCase() methods convert a string to uppercase and lowercase, respectively. They are useful for manipulating the case of characters in a string. Here’s an example:

String text = "Hello, Java!";
String uppercase = text.toUpperCase();
String lowercase = text.toLowerCase();
System.out.println("Uppercase: " + uppercase);
System.out.println("Lowercase: " + lowercase);
Uppercase: HELLO, JAVA!
Lowercase: hello, java!

equals(String obj)

Compares this string to the specified String object.

String str1 = "Hello";
String str2 = "Hello";
boolean isEqual = str1.equals(str2);
System.out.println("Are the strings equal? " + isEqual);
Are the strings equal? true

equalsIgnoreCase(String anotherString)

Compares this string to another string, ignoring case considerations.

String str1 = "Hello";
String str2 = "hello";
boolean isEqual = str1.equalsIgnoreCase(str2);
System.out.println("Are the strings equal? " + isEqual);
Are the strings equal? true

isEmpty()

Returns true if the string is empty (contains no characters), otherwise returns false.

String str = "";
boolean isEmpty = str.isEmpty();
System.out.println("Is the string empty? " + isEmpty);
Is the string empty? true

startsWith(String prefix)

Checks if the string starts with the specified prefix.

String str = "Hello, World!";
boolean startsWith = str.startsWith("Hello");
System.out.println("Does the string start with 'Hello'? " + startsWith);
Does the string start with 'Hello'? true

endsWith(String suffix)

Checks if the string contains the specified sequence of characters.

String str = "Hello, World!";
boolean endsWith = str.endsWith("World!");
System.out.println("Does the string end with 'World!'? " + endsWith);
Does the string end with 'World!'? true

replace(char oldChar, char newChar)

Replaces all occurrences of the specified character with a new character.

String str = "Hello, World!";
String replaced = str.replace('o', 'a');
System.out.println("String after replacement: " + replaced);
String after replacement: Hella, Warld!

replaceAll(String regex, String replacement)

Replaces all occurrences of a specified regex pattern with a new string.

String str = "Hello, Java!";
String replaced = str.replaceAll("a", "o");
System.out.println("String after replacement: \"" + replaced + "\"");
String after replacement: "Hello, Jovo!"

trim()

Removes leading and trailing whitespace from the string.

String str = "   Hello, World!   ";
String trimmed = str.trim();
System.out.println("Trimmed string: \"" + trimmed + "\"");
Trimmed string: "Hello, World!"

contains(CharSequence sequence)

Checks if the string contains the specified sequence of characters.

String str = "Hello, World!";
boolean contains = str.contains("World");
System.out.println("Does the string contain 'World'? " + contains);
Does the string contain 'World'? true

valueOf(Object obj)

Converts the specified object to its string representation.

int number = 42;
String str = String.valueOf(number);
System.out.println("String representation of the number: \"" + str + "\"");
String representation of the number: "42"

Complete Code : Java String Methods

Here’s a simple Java program that demonstrates the usage of the above mentioned Java String methods:

public class StringMethodsExample {
    public static void main(String[] args) {
        // equals(Object obj)
        String str1 = "Hello";
        String str2 = "Hello";
        boolean isEqual = str1.equals(str2);
        System.out.println("Are the strings equal? " + isEqual);

        // equalsIgnoreCase(String anotherString)
        String str3 = "Hello";
        String str4 = "hello";
        boolean isIgnoreCaseEqual = str3.equalsIgnoreCase(str4);
        System.out.println("Are the strings equal (ignoring case)? " + isIgnoreCaseEqual);

        // isEmpty()
        String str5 = "";
        boolean isEmpty = str5.isEmpty();
        System.out.println("Is the string empty? " + isEmpty);

        // startsWith(String prefix)
        String str6 = "Hello, World!";
        boolean startsWith = str6.startsWith("Hello");
        System.out.println("Does the string start with 'Hello'? " + startsWith);

        // endsWith(String suffix)
        String str7 = "Hello, World!";
        boolean endsWith = str7.endsWith("World!");
        System.out.println("Does the string end with 'World!'? " + endsWith);

        // contains(CharSequence sequence)
        String str8 = "Hello, World!";
        boolean contains = str8.contains("World");
        System.out.println("Does the string contain 'World'? " + contains);

        // replace(char oldChar, char newChar)
        String str9 = "Hello, World!";
        String replaced = str9.replace('o', 'a');
        System.out.println("String after replacement: " + replaced);

        // trim()
        String str10 = "   Hello, World!   ";
        String trimmed = str10.trim();
        System.out.println("Trimmed string: \"" + trimmed + "\"");

        // replaceAll(String regex, String replacement)
        String str11 = "Hello, Java!";
        String replacedAll = str11.replaceAll("a", "o");
        System.out.println("String after replacement: \"" + replacedAll + "\"");

        // valueOf(Object obj)
        int number = 42;
        String str12 = String.valueOf(number);
        System.out.println("String representation of the number: \"" + str12 + "\"");

        // length()
        String text = "Hello, Java!";
        int length = text.length();
        System.out.println("Length of the string: " + length);

        // charAt(int index)
        char character = text.charAt(7);
        System.out.println("Character at index 7: " + character);

        // substring(int beginIndex)
        String substring1 = text.substring(7);
        System.out.println("Substring from index 7: " + substring1);

        // substring(int beginIndex, int endIndex)
        String substring2 = text.substring(7, 11);
        System.out.println("Substring from index 7 to 10: " + substring2);

        // concat(String str)
        String greeting = "Hello";
        String name = "Java";
        String message = greeting.concat(", " + name + "!");
        System.out.println("Concatenated message: " + message);

        // indexOf(String str)
        int index = text.indexOf("Java");
        System.out.println("Index of 'Java': " + index);

        // toUpperCase() and toLowerCase()
        String uppercase = text.toUpperCase();
        String lowercase = text.toLowerCase();
        System.out.println("Uppercase: " + uppercase);
        System.out.println("Lowercase: " + lowercase);
    }
}
Are the strings equal? true
Are the strings equal (ignoring case)? true
Is the string empty? true
Does the string start with 'Hello'? true
Does the string end with 'World!'? true
Does the string contain 'World'? true
String after replacement: Hella, Warld!
Trimmed string: "Hello, World!"
String after replacement: "Hello, Jovo!"
String representation of the number: "42"
Length of the string: 13
Character at index 7: J
Substring from index 7: Java!
Substring from index 7 to 10: Java
Concatenated message: Hello, Java!
Index of 'Java': 7
Uppercase: HELLO, JAVA!
Lowercase: hello, java!

Conclusion: Java String Methods

In this comprehensive guide, we explored some of the most important methods available in the Java String class. We covered methods like length(), charAt(), substring(), concat(), indexOf(), toUpperCase(), and toLowerCase(). By mastering these methods, you can effectively manipulate and process strings in your Java projects.

By applying these methods in your Java code, you can handle string-related operations with ease and build robust applications that efficiently handle textual data. The Java String class’s extensive method set empowers you to create powerful string processing algorithms and solve various real-world programming challenges.

Leave a Reply

Your email address will not be published. Required fields are marked *