How to get the nth character of a string in Java
In this post we will give you information about How to get the nth character of a string in Java. Hear we will give you detail about How to get the nth character of a string in JavaAnd how to use it also give you demo for it if it is necessary.
we will learn how to get the nth character of a string in Java.
In Java, strings are the sequence of characters, where the index of the first character is 0, the second character is 1 etc.
Getting the nth character
To access the nth character of a string, we can use the built-in charAt() method in Java.
The charAt() method takes the character index as an argument and return its value in the string.
Here is an example, that gets the first characterB from the following string:
String country = "Brazil";char firstChar = country.charAt(0);System.out.println(firstChar);
Output:
"B"
We can access the second and thrid characters of a string like this:
string secondChar = country.charAt(1); // rstring thirdChar = country.charAt(2); // a
Hope this code and post will helped you for implement How to get the nth character of a string in Java. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs