Tutorial Gateway

  • C
  • C#
  • Java
  • Python
  • SQL
  • MySQL
  • Js
  • BI Tools
    • Informatica
    • Talend
    • Tableau
    • Power BI
    • SSIS
    • SSRS
    • SSAS
    • MDX
    • R Tutorial
    • Alteryx
    • QlikView
  • More
    • C Programs
    • C++ Programs
    • Python Programs
    • Java Programs

Java charAt Method

by suresh

The Java charAt method is one of the Java String Methods, which is to return the character at the specified index position. In this article, we will show how to write string charAt in Java Programming language with example.

Java charAt syntax

The basic syntax of the string charAt in Java Programming language is as shown below.

public char chatAt(int Index_Position) 

//In order to use in program
String_Object.charAt(int Index_Position)
  • String_Object: Please specify the valid String Object.
  • Index_Position: Please specify the index position of the desired character.

Return Value

The Java charAt Function will return the Character from String_Object at specified Index_Position. If we specify the Index position out of range, then the chartAt Function will throw an error.

TIP: The position of an index for Java charAt string Function will start from 0, Not 1.

Java charAt Example

In this Java program, We are going to use the Java string charAt method is used to return the Character at the given index position.

package StringFunctions;

public class CharAtMethod {

 public static void main(String[] args) {
 String str = "Learn Java Tutorial";
 
 char str1 = str.charAt(0);
 char str2 = str.charAt(6);
 char str3 = str.charAt(11);
 char str4 = str.charAt(str.length()- 1);
 
 System.out.println( "Charcter at Index position 0 = " + str1);
 System.out.println( "Charcter at Index position 6 = " + str2);
 System.out.println( "Charcter at Index position 11 = " + str3);
 System.out.println( "Charcter at Last Index position = " + str4);
 }
}

OUTPUT

Java charAt Method 1

ANALYSIS

Within this Java charAt function example, we declared the String variable and assigned a value using the following statement

String str = "Learn Java Tutorial";

The following statements will find Characters at index positions 0, 6, 11, and store the characters in str1, str2, and str3 variables of type char.

char str1 = str.charAt(0);
char str2 = str.charAt(6);
char str3 = str.charAt(11);

NOTE: For the charAt String Method, you should count the space as One Character.

In the next line, we are using the length function to find the string length.

char str4 = str.charAt(str.length()- 1);

From the above Java code snippet, we are subtracting one from string length. Because, length of a string will count from 1 to n where the index position will start from 0 to n – 1.

The following System.out.println statements will print the output

System.out.println( "Charcter at Index position 0 = " + str1);
System.out.println( "Charcter at Index position 6 = " + str2);
System.out.println( "Charcter at Index position 11 = " + str3);
System.out.println( "Charcter at Last Index position = " + str4);

String charAt Example 2

In this string charAt program, We are going to ask the user to enter Y or N. 

Based on the user entered character, the Java charAt program will display the message.

package StringFunctions;

import java.util.Scanner;

public class CharAtMethod2 {
 private static Scanner sc;

 public static void main(String[] args) {
 sc = new Scanner(System.in);
 
 System.out.println("\n Please Enter Y or N letter: ");
 String letter = sc.next();
 char ch = letter.charAt(0);
 
 if (ch == 'y' || ch == 'Y') {
 System.out.println("Hey!! Welcome to String Library");
 }
 else {
 System.out.println("Goodbye to String Library");
 }
 }
}

OUTPUT

Java charAt Method 2

Let us enter different character as the Java chartAt argument

Java charAt Method 3

ANALYSIS

The following statement will ask the user to enter Y or N character. Next, we are assigning the user entered value to the letter variable.

System.out.println("\n Please Enter Y or N letter: ");
String letter = sc.next();

Here, we are checking the character at the Zero index position using the java string charAt method.

char ch = letter.charAt(0);

Next, we used the If Else Statement to check where the character at index position zero is y / Y or not.

  • If it y or Y System.out.println(“Hey!! Welcome to String Library”); statement will be printed
  • Else System.out.println(“Goodbye to String Library”); statement will be printed

NOTE: we can use the lowercase method to convert user input to lower also, but to make the code easy, we are going in the standard way.

Placed Under: Java

  • SQL DML, DDL, DCL & TCL Cmds
  • SQL NOT EXISTS Operator
  • SQL UPDATE from SELECT
  • SQL AFTER UPDATE Triggers
  • SQL Get Column Names in Table
  • SQL IF ELSE
  • SQL ACID Properties
  • SQL FOR XML PATH
  • Java Two Dimensional Array
  • Java Perfect Number Program
  • Java Count Digits in a Number
  • C Compare Two Strings Program
  • C Print Prime Numbers 1 to 100
  • C program to Reverse a String
  • C Palindrome Number Program
  • C Program for Palindrome String
  • C Remove Duplicate String Chars
  • C Square of a Number Program
  • C Sum and Average of N Number
  • Python Fibonacci Series program
  • Python Area Of Circle Program
  • Python Prime Numbers 1 to 100
  • Python Program for Leap Year
  • Tableau Rank Calculation
  • Tableau Google Maps usage
  • Power BI Format Dates
  • Power BI Top 10 Filters
  • Power BI – Create Hierarchy
  • Power BI DAX Math Functions
  • Learn SSIS in 28 Days
  • SSIS Transformations
  • SSIS Incremental Load
  • SSRS Drill Through Reports
  • SSRS Drill Down Reports
  • R Programming Tutorial
  • C Tutorial
  • C# Tutorial
  • Java Tutorial
  • JavaScript Tutorial
  • Python Tutorial
  • MySQL Tutorial
  • SQL Server Tutorial
  • R Tutorial
  • Power BI Tutorial
  • Tableau Tutorial
  • SSIS Tutorial
  • SSRS Tutorial
  • Informatica Tutorial
  • Talend Tutorial
  • C Programs
  • C++ Programs
  • Java Programs
  • Python Programs
  • MDX Tutorial
  • SSAS Tutorial
  • QlikView Tutorial

Copyright © 2021 | Tutorial Gateway· All Rights Reserved by Suresh

Home | About Us | Contact Us | Privacy Policy