There are two variants of substring() method.This article depicts about all of them, as follows :
1. String substring() : This method has two variants and returns a new string that is a substring of this string. The substring begins with the character at the specified index and extends to the end of this string.
Syntax : public String substring(int begIndex) Parameters : begIndex : the begin index, inclusive. Return Value : The specified substring.
The extracted substring is : geeksforgeeks
2. String substring(begIndex, endIndex):This method has two variants and returns a new string that is a substring of this string. The substring begins with the character at the specified index and extends to the end of this string or up to endIndex – 1, if the second argument is given.
Syntax : public String substring(int begIndex, int endIndex) Parameters : beginIndex : the begin index, inclusive. endIndex : the end index, exclusive. Return Value : The specified substring.
The extracted substring is : geeks
Possible application : The substring extraction finds its use in many application including prefix and suffix extraction. For example to extract a lastname from name or extract only denomination from a string containing both amount and currency symbol. The latter one is explained below.
The original string is : Rs 1000 The extracted substring is : 1000
This article is contributed by Astha Tyagi. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Searching characters and substring in a String in Java
- StringBuffer substring() method in Java with Examples
- StringBuilder substring() method in Java with examples
- Java substring() method memory leak issue and fix
- Longest Common Substring in an Array of Strings
- How to change the Text Color of a Substring in android using SpannableString class?
- Java.util.LinkedList.poll(), pollFirst(), pollLast() with examples in Java
- Java.util.concurrent.RecursiveTask class in Java with Examples
- Java.util.BitSet class methods in Java with Examples | Set 2
- Java.util.Collections.disjoint() Method in java with Examples
- Java Deprecated API Scanner tool (jdepscan) in Java 9 with Examples
- Java.util.Collections.rotate() Method in Java with Examples
- Java.util.function.DoublePredicate interface in Java with Examples
- Java lang.Long.numberOfTrailingZeros() method in Java with Examples
- Java.util.function.BiPredicate interface in Java with Examples
- Java lang.Long.highestOneBit() method in Java with Examples
- Java lang.Long.numberOfLeadingZeros() method in Java with Examples
- Java lang.Long.lowestOneBit() method in Java with Examples
- Java.util.function.IntPredicate interface in Java with Examples
- Java.util.concurrent.RecursiveAction class in Java with Examples