Lesson 18 of 50 – Java String Methods
36%

Java String Methods

Java provides many built-in String methods to work with text. These methods help us find the length of a String, change its case, search for text, extract characters, replace text, and compare String values.

1. What Are String Methods?

String methods are predefined methods of the Java String class. They are called using a String variable followed by a dot and the method name.

public class Main {
    public static void main(String[] args) {
        String name = "Rahul";

        System.out.println(name.length());
        System.out.println(name.toUpperCase());
        System.out.println(name.toLowerCase());
    }
}
Output:
5
RAHUL
rahul

The methods return information or a result based on the String on which they are called.

2. length() Method

The length() method returns the number of UTF-16 code units in a String. For basic English text, this normally matches the number of characters.

public class Main {
    public static void main(String[] args) {
        String name = "Amit";
        String city = "New Delhi";

        System.out.println(name.length());
        System.out.println(city.length());
    }
}
Output:
4
9

Spaces are included when calculating String length.

3. toUpperCase() and toLowerCase()

These methods return a String converted to uppercase or lowercase.

public class Main {
    public static void main(String[] args) {
        String text = "Java Programming";

        System.out.println(text.toUpperCase());
        System.out.println(text.toLowerCase());
    }
}
Output:
JAVA PROGRAMMING
java programming

These methods do not modify the original String. Java Strings are immutable.

4. equals() and equalsIgnoreCase()

Use equals() to compare String contents. Use equalsIgnoreCase() when letter case should not affect the comparison.

public class Main {
    public static void main(String[] args) {
        String a = "Java";
        String b = "Java";
        String c = "java";

        System.out.println(a.equals(b));
        System.out.println(a.equals(c));
        System.out.println(a.equalsIgnoreCase(c));
    }
}
Output:
true
false
true
Remember: == checks whether two references refer to the same object, while equals() compares String contents.
5. charAt() Method

The charAt() method returns the character at a specified index. String indexing starts at zero.

public class Main {
    public static void main(String[] args) {
        String word = "JAVA";

        System.out.println(word.charAt(0));
        System.out.println(word.charAt(1));
        System.out.println(word.charAt(3));
    }
}
Output:
J
A
A
Index Character
0J
1A
2V
3A

An invalid index causes a StringIndexOutOfBoundsException.

6. contains() Method

The contains() method checks whether a String contains a specified sequence of characters. It returns a Boolean value.

public class Main {
    public static void main(String[] args) {
        String message = "I am learning Java";

        System.out.println(message.contains("Java"));
        System.out.println(message.contains("Python"));
        System.out.println(message.contains("learning"));
    }
}
Output:
true
false
true

The search is case-sensitive. For example, "Java" and "java" are different sequences.

7. indexOf() and lastIndexOf()

indexOf() finds the first occurrence of a character or substring. lastIndexOf() finds its last occurrence.

public class Main {
    public static void main(String[] args) {
        String text = "Java Programming Java";

        System.out.println(text.indexOf("Java"));
        System.out.println(text.lastIndexOf("Java"));
        System.out.println(text.indexOf("Python"));
    }
}
Output:
0
17
-1

If the searched text is not found, indexOf() and lastIndexOf() return -1.

8. substring() Method

The substring() method extracts part of a String.

public class Main {
    public static void main(String[] args) {
        String text = "Programming";

        System.out.println(text.substring(0, 7));
        System.out.println(text.substring(3));
    }
}
Output:
Program
gramming

In substring(start, end), the start index is included and the end index is excluded.

Example: "Programming".substring(0, 3) returns "Pro".
9. replace() Method

The replace() method returns a new String with matching characters or character sequences replaced by another value.

public class Main {
    public static void main(String[] args) {
        String text = "I like Python";

        String newText = text.replace("Python", "Java");

        System.out.println(text);
        System.out.println(newText);
    }
}
Output:
I like Python
I like Java

The original String remains unchanged. The returned String contains the replacement.

10. trim() and strip() Methods

These methods remove whitespace from the beginning and end of a String.

public class Main {
    public static void main(String[] args) {
        String name = "   Rahul Kumar   ";

        System.out.println(name.trim());
        System.out.println(name.strip());
    }
}
Output:
Rahul Kumar
Rahul Kumar

trim() removes leading and trailing characters up to U+0020. strip() uses Unicode whitespace rules and is available in Java 11 and later.

11. startsWith() and endsWith()

The startsWith() method checks the beginning of a String. The endsWith() method checks its ending.

public class Main {
    public static void main(String[] args) {
        String file = "java_program.java";

        System.out.println(file.startsWith("java"));
        System.out.println(file.endsWith(".java"));
        System.out.println(file.endsWith(".php"));
    }
}
Output:
true
true
false

These methods are useful when checking file extensions, prefixes, or specific text patterns.

12. isEmpty(), isBlank(), and concat()

These methods help check String content and combine text.

public class Main {
    public static void main(String[] args) {
        String a = "";
        String b = "   ";
        String c = "Hello ";

        System.out.println(a.isEmpty());
        System.out.println(b.isEmpty());
        System.out.println(b.isBlank());
        System.out.println(c.concat("Java"));
    }
}
Output:
true
false
true
Hello Java
  • isEmpty() checks whether the String length is zero.
  • isBlank() checks whether the String is empty or contains only whitespace. It requires Java 11 or later.
  • concat() joins another String to the current String.
13. Practical Example: Student Name Processing

This program demonstrates several String methods in one practical example.

public class Main {
    public static void main(String[] args) {
        String firstName = "  Rahul";
        String lastName = "Kumar  ";

        firstName = firstName.strip();
        lastName = lastName.strip();

        String fullName = firstName.concat(" ").concat(lastName);

        System.out.println("Full Name: " + fullName);
        System.out.println("Name Length: " + fullName.length());
        System.out.println("Uppercase: " + fullName.toUpperCase());
        System.out.println("Lowercase: " + fullName.toLowerCase());
        System.out.println("Starts with Rahul: " +
                fullName.startsWith("Rahul"));
        System.out.println("Contains Kumar: " +
                fullName.contains("Kumar"));
    }
}
Output:
Full Name: Rahul Kumar
Name Length: 11
Uppercase: RAHUL KUMAR
Lowercase: rahul kumar
Starts with Rahul: true
Contains Kumar: true
Lesson Summary:
  • length() counts UTF-16 code units.
  • toUpperCase() and toLowerCase() change letter case in the returned String.
  • equals() compares String contents.
  • charAt() retrieves a character at an index.
  • contains() searches for text.
  • indexOf() and lastIndexOf() find positions.
  • substring() extracts part of a String.
  • replace() returns a String with replacements.
  • trim() and strip() remove leading and trailing whitespace according to their respective rules.

🧠 Quick Quiz

Q1. Which method returns the length of a String?



Q2. Which method compares String contents?



Q3. What does "Java".toUpperCase() return?