KnowledgeBoat Logo

Computer Applications

Differentiate between charAt() and substring()

Java String Handling

40 Likes

Answer

charAt()substring()
It returns a character from the string at the index specified as its argumentIt extracts a part of the string as specified by its arguments and returns the extracted part as a new string.
Its return type is charIts return type is String
Example:
String str = "Hello";
char ch = str.charAt(1);
System.out.println(ch);

Output of this code snippet is e.
Example:
String str = "Hello";
String subStr = str.substring(1);
System.out.println(subStr);

Output of this code snippet is ello.

Answered By

22 Likes


Related Questions