
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
Java Program to Get a Character From the Given String
In this article, we will understand how to get a character from the given string. Char is a datatype that contains an alphabets or an integer or an special character. String is a datatype that contains one or more characters and is enclosed in double quotes(“ ”).
Below is a demonstration of the same −
Suppose our input is −
Input string: Java Programming Index: 11
The desired output would be −
Result: m
Algorithm
Step 1 - START Step 2 - Declare a string value namely input_string and a char value namely resultant_character. Step 3 - Define the values. Step 4 - Using the function string.charAt(), fetch the char value present at the specified position. Store the value in resultant_character. Step 5 - Display the result Step 6 - Stop
Example 1
Here, we bind all the operations together under the ‘main’ function.
public class CharacterAndString { public static void main(String[] args) { String string = "Java Programming"; System.out.println("The string is defined as " +string); int index = 11; char resultant_character = string.charAt(index); System.out.println("\nA character from the string :" + string + " at index " + index + " is: " + resultant_character); } }
Output
The string is defined as Java Programming A character from the string :Java Programming at index 11 is: m
Example 2
Here, we encapsulate the operations into functions exhibiting object-oriented programming.
public class CharacterAndString { public static char get_chararacter(String string, int index) { return string.charAt(index); } public static void main(String[] args) { String string = "Java Programming"; System.out.println("The string is defined as " +string); int index = 11; char resultant_character = get_chararacter(string, index); System.out.println("\nA character from the string :" + string + " at index " + index + " is: " + resultant_character); } }
Output
The string is defined as Java Programming A character from the string :Java Programming at index 11 is: m
- Related Articles
- Java program to find the Frequency of a character in a given String
- Java Program to create Character Array from String Objects
- Java Program to get a character located at the String's specified index
- Java Program to replace all occurrences of a given character in a string
- How to match a character from given string including case using Java regex?
- Java program for removing n-th character from a string
- C# program to replace n-th character from a given index in a string
- Java Program to access character of a string
- Java program to delete duplicate characters from a given String
- C# Program to change a character from a string
- Java Program to locate a character in a string
- Java program to convert a character array to string
- Java Program to Convert Character to String
- C# program to get max occurred character in a String
- Java program to remove all the white spaces from a given string

Advertisements