![java substring java substring](https://www.developerhelps.com/wp-content/uploads/2019/09/Java_String_Split.jpg)
Note that the charAt() method throws an IndexOutOfBoundsException with this condition: if the index parameter is greater than or equal to the string’s length or if it is negative. ("The first character of the String is: " + firstCharacter) We know that strings follow zero-based indexing, and the first character is always present at index 0.
#Java substring how to#
The program below shows how to use this method to fetch the first character of a string. The String class method and its return type are a char value.
![java substring java substring](https://www.edureka.co/blog/wp-content/uploads/2017/05/String-pool-1.png)
The charAt() method takes an integer index value as a parameter and returns the character present at that index. Get the First Character Using the charAt() Method in Java Let’s learn the different ways of fetching the first character from a string. Strings can be used for various purposes, and we may need to access their different characters to manipulate them.įor example, if we have a string array of first names and we want to make sure that the first character of each name is capitalized, we need to access the first character of each string. We also listed some example codes to help you understand the topic.Ī string is simply a collection or sequence of characters in Java. This tutorial introduces how to get the first character of a string in Java.
![java substring java substring](https://www.softwaretestinghelp.com/wp-content/qa/uploads/2020/05/Java-String-substring-method.png)
Get the First Character Using the substring() Method in Java.Get the First Character Using the String substring() Method in Java.Get the First Character Using the charAt() Method With No Exception in Java.Get the First Character Using the charAt() Method in Java.