CodesJava

Easy learning with example program codes

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

s

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.

 

Please follow and like us:
error
Posted in Java   








Copyright © 2019 CodesJava DMCA.com Protection Status