In this article, we will understand with a Java program on how to get sub-string from a String using Java 1.8 version
Already in one of the previous article, we discussed how to get sub-string from a String
Get sub-string from a String :
- substring() method of String
- This String method returns substring for the specified begin value (start-index) and end value (end-index)
- There are 2 variants or overloaded substring() methods, in addition to this there is subSequence() method which is very much same like 2nd variant but legacy and CharSequece
- 1st variant – returns substring starting from specified index-position till length
- 2nd variant – returns substring starting from specified index-position to specified end index-position
- 3rd variant – returns substring starting from specified index-position to specified end index-position
- Method signature of 3 variants are,
Method signature:
public String substring(int beginIndex);
public String substring(int beginIndex, int endIndex);
public CharSequence subSequence(int beginIndex, int endIndex);
GetSubstringFromString.java
package in.bench.resources.java8.string.methods;
import java.util.stream.Stream;
public class GetSubstringFromString {
public static void main(String[] args) {
// test string
String str = "BenchResources";
// 1st variant - specify only start index-position
String subString1 = Stream
.of(str)
.map(s -> s.substring(5))
.findAny()
.get();
System.out.println("Sub-string for starting with 5th index-position is = "
+ subString1);
// 2nd variant - specify start/end index-position
String subString2 = Stream
.of(str)
.map(s -> s.substring(7, 13))
.findAny()
.get();
System.out.println("\nSub-string for starting-with 7th index & ending-with 12th index is = "
+ subString2);
// 3rd variant - specify start/end index-position
CharSequence subSequence3 = Stream
.of(str)
.map(s -> s.subSequence(0, 5))
.findAny()
.get();
System.out.println("\nSub-sequence for starting-with 0th index & ending-with 4th index is = "
+ subSequence3);
}
}
Output:
Sub-string for starting with 5th index-position is = Resources
Sub-string for starting-with 7th index & ending-with 12th index is = source
Sub-sequence for starting-with 0th index & ending-with 4th index is = Bench
Related Articles:
- Java 8 – How to get a specific character from String ?
- Java 8 – How to check whether particular word/letter/sub-string is present in the String ?
- Java 8 – How to check whether particular String endsWith specific word/letter ?
- Java 8 – How to check whether particular String startsWith specific word/letter ?
- Java 8 – How to check whether a String is empty or not ?
- Java 8 – How to get length of a String ?
- Java 8 – How to convert a String into char[] Arrays ?
- Java 8 – How to convert a String into UpperCase String ?
- Java 8 – How to convert a String into LowerCase String ?
- Java 8 – How to remove leading and trailing whitespaces in a String ?
- Java 8 – How to replace a String with another String ?
- Java 8 – How to split a String based on delimiter ?
- Java 8 – How to convert primitive data-types into String ?
- Java 8 – How to join String[] Arrays elements using different delimiter ?
- Java 8 – How to join List of String elements using different delimiter ?
- Java 8 – How to find 1st and last index of particular character/substring in a String ?
- Java 8 – How to get hashCode of a String ?
- Java 8 – How to get sub-string from a String ?
References:
- https://docs.oracle.com/javase/8/docs/api/java/lang/String.html
- https://docs.oracle.com/javase/8/docs/api/java/util/stream/Stream.html
Happy Coding !!
Happy Learning !!