Home Java Beginners CharAt() in Java

 
 

Share on Google+Share on Google+

CharAt() in Java

Advertisement
Here we will discuss about charAt() method of String class. It is used to retrieve the character position in a String.

In this section, we will discuss about charAt() method of String class and how it is used. It comes in java.lang.String package. charAt() method returns a character value for the character at the given position of the string. Index value of charAt() method starts from 0 and ends at length-1 of the String. It throws IndexOutOfBoundsException if value of index is negative or greater than the length of the String.

Syntax:

public char charAt(int index)

In the following example we have used charAt() method to find the character at the 4th position of the string and retrieve it.

Example of charAt() in Java:

import java.io.*;
class CharAt{
public static void main(String[] args){
try{
BufferedReader object=
new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the String");
String s=object.readLine();
int len=s.length();
System.out.println(len);
char char1=s.charAt(4);
System.out.println(char1);
}
catch(Exception e){}
}
}

Output:

C:\java_work>java CharAt

Enter the String
roseindia
9
i

C:\java_work>

Resource:

Advertisement

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: October 9, 2013

Related Tutorials

Discuss: CharAt() in Java  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:0
DMCA.com