Difference Between charAt() and substring() Method in Java Last Updated : 27 Feb, 2024 Comments Improve Suggest changes Like Article Like 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 JavaThe 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 JavaLet 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 } } Outputp 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)); } } OutputPawan 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. Comment More infoAdvertise with us Next Article Difference Between charAt() and substring() Method in Java D d_singh Follow Improve Article Tags : Java Java-Strings Java-Functions Practice Tags : JavaJava-Strings Similar Reads Difference between String and Character array in Java Unlike C/C++ Character arrays and Strings are two different things in Java. Both Character Arrays and Strings are a collection of characters but are different in terms of properties. Differences between Strings and Character Arrays:PropertyStringCharacter ArrayDefinitionA sequence of characters is r 3 min read Difference Between StringTokenizer and Split Method in Java Legacy classes and interfaces are the classes and interfaces that formed the Collection Framework in the earlier versions of Java and how now been restructured or re-engineered. Splitting of String is basically breaking the string around matches of the given regular expression. Strings can be split 3 min read Difference Between Length and Capacity in Java Length and capacity are two different things in Java. Length basically increases when characters are appended to the string whereas the capacity increases when the current capacity is exceeded by a new length. length () method is a part of the Java String class. It is used to find the length of the 3 min read Difference Between CHAR And VARCHAR In SQL Server In SQL Server, CHAR and VARCHAR are used to store character strings, with CHAR being a fixed-length data type and VARCHAR a variable-length one. CHAR ensures consistent storage size for all entries while VARCHAR adjusting to varying string lengths, optimizing storage efficiency.In this article, We w 6 min read Different Ways to Generate String by using Characters and Numbers in Java Given a number num and String str, the task is to generate the new String by extracting the character from the string by using the index value of numbers. Examples: Input: str = âGeeksforGeeksâ num = 858 Output: GfG Explanation: The 8th, 5th, and 8th position of the characters are extracting from th 3 min read StringBuffer deleteCharAt() Method in Java with Examples The Java.lang.StringBuffer.deleteCharAt() is a built-in Java method which removes the char at the specified position in this sequence. So that the sequence is reduced by 1 char. Syntax: public StringBuffer deleteCharAt(int indexpoint) Parameters : The method accepts a single parameter indexpoint of 2 min read Java StringBuffer deleteCharAt() Method In Java, the deleteCharAt() method of the StringBuffer class is used to delete the character at a specified index in the string. Example 1: The below Java program demonstrates the use of deleteCharAt() to remove a character at a specific index from the string.Java// Removing a character at a specifi 2 min read Java String charAt() Method String charAt() method in Java returns the character at the specified index in a string. The Index of the first character in a string is 0, the second character is 1, and so on. The index value should lie between 0 and length() - 1.If the index value is greater than or equal to the string length or 2 min read Searching For Characters and Substring in a String in Java Efficient String manipulation is very important in Java programming especially when working with text-based data. In this article, we will explore essential methods like indexOf(), contains(), and startsWith() to search characters and substrings within strings in Java.Searching for a Character in a 5 min read StringWriter append(char) method in Java with Examples The append(char) method of StringWriter Class in Java is used to append the specified char value on the writer. This char value is taken as a parameter. Syntax: public void append(char charValue) Parameters: This method accepts a mandatory parameter charValue which is the char value to be appended o 2 min read Like