Open In App

Difference Between charAt() and substring() Method in Java

Last Updated : 27 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

In Java, the charAt() method of the String class is used to extract the character from a string. It returns the character at the specified index in the String. The substring() method is used to extract some portion from the actual String and the actual string remains the same as it is. After that, the method returns a new string.

In this article, we will learn charAt() vs substring() methods in Java.

charAt() Method in Java

The charAt() method returns characters at the specific index in a String. The indexing starts from 0 i.e. the first character’s index is 0 then 1 and so on. But the index of the last character is length() – 1.

Syntax of charAt() Method:

char charAt(int index)

Example of charAt() method in Java

Let us understand this method by implementing a simple example below. If we want to get the character at a specific position in a string:

Java




// Java program to demonstrate
// charAt() method of String class
import java.io.*;
public class StringExample
{
    public static void main(String []args)
    {
       String s = "Deepshikha";    //String
       System.out.println(s.charAt(3));     // prints character at index 3
       System.out.println(s.charAt(6));     // prints character at index 6
          
    }
}


Output

p
i

Explanation of the Program:

  • We have a String, in which we have performed the implementation of charAt() method.
  • The index is zero based, so the s.charAt(3) prints “p” and s.charAt(6) prints “i“.

substring() method in Java

The substring() method is used to extract some portion from the actual String and the actual string remains the same. This method returns a new string. We can say this a subset of a String. There are two variants in subString() method:

  • substring (int start Index): It returns the subset of the String (a new string), and it starts from the given start index to the end of the string.
  • substring(int start index, int end Index): It starts from the given start index and stops at the end index, this means both start index and end Index are part of the substring that is extracted.

Syntax of substring() Method:

String.substring(int index)
String.substring(int start index, int end Index)

Example of substring() method in Java

Java




// Java program to demonstrate 
// substring() method of String class
import java.io.*;
  
public class SubString 
{
    public static void main(String args[]) 
    {
        String s = "Deep Pawan";       //String
        System.out.println(s.substring(5));  
        System.out.println(s.substring(0,4));  
        System.out.println(s.substring(2,4));  
    }
}


Output

Pawan
Deep
ep

Explanation of the above Program:

  • In a String, we have performed the implementation of substring() method.
  • s.substring(5) prints a substring starts from index 5 to the end of the string.
  • s.substring(0,4) prints a substring starts from index 0 to index 4.
  • s.substring(2,4) prints a substring starts from index 2 to index 4.

Difference between charAt() and substring() method in Java

charAt() method

substring() method

It extracts a single character at a specified index.

It extracts some portion of a string based on indexes.

Its return type is char.

Its return type is String.

It returns only a single character.

It can return a sequence of characters i.e. , substring.

Example: String str = “Deep”;
char ch = str.charAt(2);
System.out.println(ch);
Output will be e.

Example: String str = “Deep”;
String subString = str.substring(1);
System.out.println(subStr);
Output will be eep.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads