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”; |
Example: String str = “Deep”; |
Difference Between charAt() and substring() Method in Java
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.
Contact Us