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 w3spoint
 */
class TestString{
	String str = "www.w3spoint.com";
 
	/**
	 * This method is used to show the use of charAt() method.
	 * @author w3spoint
	 */
	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:

o

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.

 

Content Protection by DMCA.com
Please Share