GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Java Tutorial
  • Advanced Java
  • Interview Questions
  • Exercises
  • Examples
  • Quizzes
  • Projects
  • Cheatsheet
  • DSA in Java
  • Java Collection
  • Introduction3 min read
  • Variables2 min read
  • Data Types7 min read
  • Operators7 min read

This view

  • Original page
  • New address

GeezerVison is showing www.geeksforgeeks.org — Java String Methods - GeeksforGeeks

geeksforgeeks
search icon
  • Courses
  • Tutorials
  • Interview Prep
  • Java Tutorial
  • Advanced Java
  • Interview Questions
  • Exercises
  • Examples
  • Quizzes
  • Projects
  • Cheatsheet
  • DSA in Java
  • Java Collection

Java String Methods

Last Updated : 28 Sep, 2026

Java String methods are built-in functions provided by the String class to perform various operations on strings. These methods help in manipulating, comparing, searching, and formatting text efficiently. They make string handling easier and more convenient in Java programs.

  • Help in text manipulation and processing.
  • Support string comparison.
  • Allow searching within strings.
Java
public class Geeks{
    public static void main(String[] args) {
        String str = "GeeksforGeeks";

        System.out.println("Length: " + str.length());  
        System.out.println("Uppercase: " + str.toUpperCase());
        System.out.println("Substring: " + str.substring(2, 6));
    }
}

Output
Length: 13
Uppercase: GEEKSFORGEEKS
Substring: eksf

Commonly Used Java String Methods

Java provides a rich set of String methods that help perform various operations like comparison, searching, modification of string. Let's Understand one by one.

1. int length() Method

This method provides the total count of characters in the string.

Java
public class Geeks {
    public static void main(String[] args) {
        String s = "Hello, World!";
        System.out.println(s.length());
    }
}

Output
13

2. charAt(int i) Method

This method returns the character at ith index.

Java
public class Geeks {
    public static void main(String[] args) {
        String s = "Hello, World!";
        System.out.println(s.charAt(7));
    }
}

Output
W

3. String substring(int i) Method

This method return the substring from the ith index character to end.

Java
public class Geeks {
    public static void main(String[] args) {
        String s = "Hello, World!";
        System.out.println(s.substring(7));
    }
}

Output
World!

4. String substring(int i, int j) Method

This method returns the substring from i to j-1 index.

Java
public class Geeks {
    public static void main(String[] args) {
        String s = "Hello, World!";
        System.out.println(s.substring(7, 12));
    }
}

Output
World

5. String concat( String str) Method

This method appends the given string to the end of the current string.

Java
public class Geeks {
    public static void main(String[] args) {
        String s = "Hello, World!";
        System.out.println(s.concat("!!!"));
    }
}

Output
Hello, World!!!!

6. int indexOf(String s) Method

This method returns the index within the string of the first occurrence of the specified string. If the specified string s is not found in the input string, the method returns -1 by default.

Java
public class Geeks {
    public static void main(String[] args) {
        String s = "Hello, World!";
        System.out.println(s.indexOf("World"));
    }
}

Output
7

7. int indexOf(String s, int i) Method

This method returns the index within the string of the first occurrence of the specified string, starting at the specified index.

Java
public class Geeks {
    public static void main(String[] args) {
        String str = "Hello, World!";
        System.out.println(str.indexOf("l", 4));
    }
}

Output
10

8. int lastIndexOf(String s) Method

This method returns the index within the string of the last occurrence of the specified string. If the specified string s is not found in the input string, the method returns -1 by default.

Java
public class Geeks {
    public static void main(String[] args) {
        String s = "Hello, World!";
        System.out.println(s.lastIndexOf("l"));
    }
}

Output
10

9. boolean equals(Object otherObj) Method

This method compares this string to the specified object.

Java
public class Geeks {
    public static void main(String[] args) {
        String s = "Hello, World!";
        System.out.println(s.equals("Hello, World!"));
    }
}

Output
true

10. boolean equalsIgnoreCase(String anotherString) Method

This method checks if two strings are equal, without considering letter case.

Java
public class Geeks {
    public static void main(String[] args) {
        String s = "Hello, World!";
        System.out.println(s.equalsIgnoreCase("hello, world!"));
    }
}

Output
true

11. int compareTo(String anotherString) Method

This method compares two string lexicographically.

Java
public class Geeks {
    public static void main(String[] args) {
        String s = "Hello, World!";
        System.out.println(s.compareTo("Hello, Java!"));
    }
}

Output
13

12. int compareToIgnoreCase(String anotherString) Method

This method compares two string lexicographically, ignoring case considerations.

Java
public class Geeks {
    public static void main(String[] args) {
        String s = "Hello, World!";
        System.out.println(s.compareToIgnoreCase("hello, java!"));
    }
}

Output
13

13. String toLowerCase() Method

This method converts all the characters in the String to lower case.

Java
public class Geeks {
    public static void main(String[] args) {
        String s = "Hello, World!";
        System.out.println(s.toLowerCase());
    }
}

Output
hello, world!

14. String toUpperCase() Method

This method converts all the characters in the String to upper case.

Java
public class Geeks {
    public static void main(String[] args) {
        String s = "Hello, World!";
        System.out.println(s.toUpperCase());
    }
}

Output
HELLO, WORLD!

15. String trim() Method

This method returns the copy of the String, by removing whitespaces at both ends. It does not modify the whitespace characters present between the text.

Java
public class Geeks {
    public static void main(String[] args) {
        String s = "   Hello, Trim!   ";
        System.out.println("'" + s.trim() + "'");
    }
}

Output
'Hello, Trim!'

16. String replace(char oldChar, char newChar) Method

This method returns a new string where all instances of oldChar are replaced by newChar.

Java
public class Geeks {
    public static void main(String[] args) {
        String s = "Hello, World!";
        System.out.println(s.replace('l', 'x'));
    }
}

Output
Hexxo, Worxd!

17. boolean contains(CharSequence sequence) Method

This method returns true if string contains the given string.

Java
public class Geeks {
    public static void main(String[] args) {
        String s = "Hello, World!";
        System.out.println(s.contains("World"));
    }
}

Output
true

18. char[] toCharArray() Method

This method converts the string into a new character array.

Java
public class Geeks {
    public static void main(String[] args) {
        String str = "Hello";
        char[] chars = str.toCharArray();
        for(char c : chars) {
            System.out.print(c + " ");
        }
    }
}

Output
H e l l o 

19. boolean startsWith(String prefix) Method

This method returns true if string starts with this prefix.

Java
public class Geeks {
    public static void main(String[] args) {
        String s = "Hello, World!";
        System.out.println(s.startsWith("Hello"));
    }
}

Output
true

Common String Methods in Java

String Methods

Description

int length()

Returns the number of characters in the String.

char charAt(int i)

Returns the character at ith index.

String substring (int i)

Return the substring from the ith  index character to end.

String substring (int i, int j)

Returns the substring from i to j-1 index.

String concat( String str)

Concatenates specified string to the end of this string.

int indexOf (String s)

Finds the position of the first occurrence of the given substring within the main string. If the specified string s is not found in the input string, the method returns -1 by default.

int indexOf (String s, int i)

Returns the index within the string of the first occurrence of the specified string, starting at the specified index.

int lastIndexOf( String s)

Returns the index within the string of the last occurrence of the specified string.
If the specified string s is not found in the input string, the method returns -1 by default.

boolean equals( Object otherObj)

Compares this string to the specified object.

boolean  equalsIgnoreCase (String anotherString)

Compares string to another string, ignoring case considerations.

int compareTo( String anotherString) 

Compares two string lexicographically.

int compareToIgnoreCase( String anotherString) 

Compares two string lexicographically, ignoring case considerations.

Note: In this case, it will not consider case of a letter (it will ignore whether it is uppercase or lowercase).

String toLowerCase()

Converts all the characters in the String to lower case.

String toUpperCase()

Converts all the characters in the String to upper case.

String trim()

Returns the copy of the String, by removing whitespaces at both ends. Whitespace characters between words remain unchanged.

String replace (char oldChar, char newChar)

Generates a new string where every instance of oldChar is substituted with newChar.

boolean contains(CharSequence sequence)

Returns true if string contains the given string.

char[] toCharArray()

Converts this String to a new character array.

boolean startsWith(String prefix)

Return true if string starts with this prefix.

Comment

Explore

    Basics
    • Introduction3 min read
    • Variables2 min read
    • Data Types7 min read
    • Operators7 min read
    • Decision Making5 min read
    • Methods7 min read
    • Arrays7 min read
    • Strings5 min read
    OOP & Interface
    • Introduction6 min read
    • Constructors6 min read
    • Encapsulation3 min read
    • Inheritance8 min read
    • Polymorphism5 min read
    • Abstraction5 min read
    • Interfaces8 min read
    Collections
    • Collection class9 min read
    • Collection Interface5 min read
    • List Interface8 min read
    • Set Interface5 min read
    • Queue Interface5 min read
    • Map Interface in Java6 min read
    • Iterator4 min read
    Advance Java Concepts
    • Exception handling6 min read
    • Java Multithreading Tutorial3 min read
    • Synchronization in Java9 min read
    • File handling6 min read
    • Method References3 min read
    • Java 8 Stream Tutorial7 min read
    • Memory Management4 min read
    • Garbage Collection in Java6 min read
    • Networking6 min read
    • JDBC5 min read
    Practice Java
    • Interview Questions15+ min read
    • Programs6 min read
    • Practice Problems5 min read
    • Quiz1 min read
    • Project Ideas15+ min read
    Courses
    • Java Backend Course2 min read
    • Placement 360 Course2 min read
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.