substring() - String Methods in Java


This article is a part of String Methods in Java.
The substring() method returns a substring from the string. There can one or two parameters passed into this method. The first parameter talks about the beginIndex and the second parameter talks about the endIndex. If there is only one parameter provided in the method then by default it becomes the beginIndex and the endIndex becomes length()-1.


Return type

The return type of substring() is String. Let us look at an example.


Output

Let us look at the output of the above.

String Methods substring()
Fig.1- String Method - substring()

In the first case, the beginIndex is 8, while the endIndex is 14. So, it prints all the characters from index 8 to 14, i.e “Journal.” In the second case the beginIndex is 0, while the endIndex is 14. So, it prints all the characters from index 0 to 14, i.e “LearningJournal.” In the third case the beginIndex is 0, while the endIndex is 7. So, it prints all the characters from index 0 to 7, i.e “Learnin.”

Read More

Author : Satyam Kumar -



You will also like: