Java String charAt() Method

We are going to discuss the String charAt() function in Java.

The Java String returns the character in a string at the given position charAt(int index) function. The index value should fall between 0 and (length of string-1).

Explanation

  • The character at the given index in a string is returned by the charAt() function.
  • The first character’s index is 0, the second character’s index is 1, and so forth.
  • The charAt() method of the Java String class returns a character value at the specified index.
  • The string’s length, n, is represented by the index number, which ranges from 0 to n-1.

Syntax:

public char charAt(int index)

Internal implementation
public char charAt(int index) {    
       If (index >= value.length || (index 0))   
           throw new StringIndexOutOfBoundsException(index);    
       }    
       return value[index];    
   }

Parameter

Index – An int integer that represents the character’s index to return

  • The procedure accepts the parameter index. The initial index is zero. It gives a character from a string at a given index position.
  • If the index is bigger than this string length or a negative number, a StringIndexOutOfBoundsException is thrown.

Return Value

Returns: The char value for this string at the given index.
Index 0 contains the first character value.

Signature

  • The public method charAt() has an access modifier.
  • It takes an int index as a parameter.
  • Value returned – This function returns a char value in the String object at a specific index.

Specified by

CharSequence interface, located inside java.lang package.

Exceptions

  • If the index is negative or longer than the string’s length, an exception called StringIndexOutOfBounds is thrown.
  • The StringIndexOutOfBoundsException exception is thrown when a negative integer or an out-of-bounds positive integer is given while calling Java’s charAt() method.

Diagram

java String charAt() diagram

Example 1:

public class CharAt{  
public static void main(String args[]){  
String name="Greatest";  
char ch=name.charAt(3);
System.out.println("The Character is:",ch);  
}
}

Output:
The Character is a.

Example 2:

public class CharAtExample{  
public static void main(String args[]){  
String name="Tremendous";  
char ch=name.charAt(10);
System.out.println(ch);  
}
}

Output:
String index out of range: 10
StringIndexOutOfBoundsException has occurred.

Example 3:

public class CharAtExample3 {  
    public static void main(String[] args) {  
    String str = "Great think takes time";      
    int strLength = str.length();      
    System.out.println("First Character is: "+ str.charAt(0));      
    System.out.println("Last Character is: "+ str.charAt(strLength-1));      
    }  
}

Output :
First Character is: G
Last Character is: e

Example 4:

public class CharAt{  
    public static void main(String[] args) {  
        String str = "Blue is our favourite colour";  
        int count = 0;  
        for (int i=0; i<=str.length()-1; i++) {  
            if(str.charAt(i) == 'o') {  
                count++;  
            }  
        }  
        System.out.println("Frequency of o is: "+count);  
    }  
}

Output:
Frequency of o is: 3

Difference between charAt() and indexOf()

  • The “charAt()” method returns the character to a string’s given index.
  • The “indexOf()” method returns the first occurrence of the specified character(s) or substring in the string.
  • The latter method can also be tailored to start your search from the selected index.

Used to find

  • Print All Characters of the String
  • Print the First and Last Characters of a String
  • Print Characters Presented at Odd Positions
  • Count the Number of Vowels in a String
  • Count the Occurrence

Importance of String charAt()

  • The parameter for this Java method is always of the int type.
  • This method returns the character as char in response to the supplied int argument. The int value specifies the index, which starts at 0.
  • The IndexOutOfBounds Exception issue is raised if the index value is greater than the length of the string or negative.
  • The string_length-1 to 0 range must be used as the index range.

Advantages of String charAt()

  • Indexing – It is used to specify the characters at a specific index.
  • Character access – It easily accesses the individual character within the string.
  • Efficiency – It mainly accesses the character at a specific index of 0.

Disadvantages of String charAt()

  • Index error – Sometimes, it provides an error when accessing the index.
  • Limited function – It only accesses the character and does not provide other strings or libraries.
  • Length depends –It only depends on the length of the string, and if handled incorrectly, it occurs in error.

Conclusion

We extensively learn the Java string CharAt() function method. It is used in various methods of function in characters.

When a character value is searched for in a string, Java’s built-in charAt() function delivers the character value at the specified index number.