Java String charAt Example
String class charAt method example
String class charAt method example:- This example shows how to use charAt method. this method returns the Unicode or predefined number images of characters located at the specified index.
Here is the code:-
/**
* @(#) CharAtString.java
* CharAtString class demonstrates the working of charAt() method of String class of lang package
* @version 13-May-2008
* @author Rose India Team
*/
public class CharAtString {
public static void main(String args[]){
String str = "Welcome ", str1 = "to roseindia", str3;
str3 = str + str1;
System.out.println(str3);
// charAt method returns the corresponding character to the passed index
// in parentheses
char letter = str3.charAt(0), letter1 = str3.charAt(1), letter2 = str3
.charAt(2), letter3 = str3.charAt(3), letter4 = str3.charAt(4), letter5 = str3
.charAt(5), letter6 = str3.charAt(6), letter7 = str3.charAt(7);
System.out.println("letters" + "["+ letter + "]" + " , " + " [" + letter1 + "]" + " , " + "[" + letter2 + "]"
+ " , " + "[" + letter3 + "]" + " ," + "[" + letter4 + "]" + " , " + "[" + letter5 + "]" + " , " + "["
+ letter6 + "]" + " ---are the letters at indexes 0, 1, 2, 3, 4, 5, 6, and 7 respectively");
}
}
|
Output of the program.
Welcome to roseindia
letters[W] , [e] , [l] , [c] ,[o] , [m] , [e] ---are the letters at indexes 0, 1, 2, 3, 4, 5, 6, and 7 respectively
|
|