Home » Java programming language

Java String charAt() Method with Example

Java String charAt() Method: Here, we are going to learn about the charAt() method of String class with example in Java.
Submitted by IncludeHelp, on February 08, 2019

String charAt() Method

charAt() method is a String class method in Java, it is used to get the character from specified index from a given string.

Syntax:

    char String.charAt(index);

It accepts an index and returns the characters from the specified position of the String.

Note: If the index is out of range, the method returns "String index out of range" exception.

Example:

    Input:
    str = "includeHelp"
    Function call:
    str.charAt(0)

    Output:
    "i"

Code:

public class Main
{
    public static void main(String[] args) {
        String str = "includeHelp";
        System.out.println("character at 0 index: " + str.charAt(0));
        System.out.println("character at 1 index: " + str.charAt(1));
        System.out.println("character at 2 index: " + str.charAt(2));
        System.out.println("character at 3 index: " + str.charAt(3));
        System.out.println("character at 4 index: " + str.charAt(4));
        System.out.println("character at 5 index: " + str.charAt(5));
        System.out.println("character at 6 index: " + str.charAt(6));
        System.out.println("character at 7 index: " + str.charAt(7));
        System.out.println("character at 8 index: " + str.charAt(8));
        System.out.println("character at 9 index: " + str.charAt(9));
        System.out.println("character at 10 index: " + str.charAt(10));
    }
}

Output

character at 0 index: i
character at 1 index: n
character at 2 index: c
character at 3 index: l
character at 4 index: u
character at 5 index: d
character at 6 index: e
character at 7 index: H
character at 8 index: e
character at 9 index: l
character at 10 index: p





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.
Learn PCB Designing: PCB DESIGNING TUTORIAL







Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing » Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.