• August 27, 2014
  • Core Java
  • Comments Off on charAt(int index) String function in java

charAt(int index) String function in java

charAt(int index):

Returns the char value at the specified index.

Syntax:  public char charAt(int index).

Note: An index ranges from 0 to length() – 1. If index is negative or greater than length() – 1, it will  throw IndexOutOfBoundsException. 

Example:

StringCharAtExample.java

/**
 * This program is used to show the use of charAt() method.
 * @author javawithease
 */
class TestString{
	String str = "www.javawithease.com";
 
	/**
	 * This method is used to show the use of charAt() method.
	 * @author javawithease
	 */
	public void charAtTest(){
		//Returns the character value at the specified index.
		System.out.println(str.charAt(8));
	}
}
 
public class StringCharAtExample {
	public static void main(String args[]){
		//creating TestString object
		TestString obj = new TestString();
 
		//method call
		obj.charAtTest();
	}
}

Output:

w

Download this example.
 
Next Topic: startsWith(String prefix) and endsWith(String suffix) String functions in java.
Previous Topic: Commonly used methods of String Class with example.

Related Topics:

String handling in java with example.
String Comparison in java with example.
String concatenation in java with example.
Substring in java with example.
How to write Immutable class in java with example.
toString() method in java with example.
Commonly used methods of String Class with example.
charAt(int index) String function in java with example.
startsWith(String prefix) and endsWith(String suffix) String functions in java.
indexOf(String str) and lastIndexOf(String str) String functions in java.
toLowerCase() and toUpperCase() String functions in java.
intern() String functions in java with example.
length() String functions in java with example.
trim() String functions in java with example.

 


Comments are closed.