In this article, we will discuss and learn how to get a specific character from String in Java 1.8 version
Already in one of the previous article, we discussed how to get a character from a String using earlier versions of Java like 5 or 7, etc.
Get a specific character from String:
- charAt(index) method of String
- Retrieves a character at the specified index-position from the String
- If specified index-position is within the range i.e., 0 <= index <= (str.length -1) then it retrieves a character
- Otherwise, java.lang.StringIndexOutOfBoundsException is thrown for out-of-index character
GetACharacterFromString.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | package in.bench.resources.java8.string.methods; import java.util.stream.Stream; public class GetACharacterFromString { public static void main(String[] args) { // string String str = "BenchResources.Net" ; // 1. get character at 5th index-position char ch1 = Stream.of(str).map(s -> s.charAt( 5 )).findFirst().get(); System.out.println( "char at 5th index-position is = " + ch1); // 2. get character at 15th index-position char ch2 = Stream.of(str).map(s -> s.charAt( 15 )).findFirst().get(); System.out.println( "char at 15th index-position is = " + ch2 + "\n\n" ); // 3. exception - out of index char chEx = Stream.of(str).map(s -> s.charAt( 18 )).findFirst().get(); System.out.println( "char at 18th index-position is = " + chEx); } } |
Output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | char at 5th index-position is = R char at 15th index-position is = N Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 18 at java.base/java.lang.StringLatin1.charAt(StringLatin1.java: 48 ) at java.base/java.lang.String.charAt(String.java: 1512 ) at in.bench.resources.java8.string.methods.GetACharacterFromString .lambda$ 2 (GetACharacterFromString.java: 24 ) at java.base/java.util.stream.ReferencePipeline$ 3 $ 1 .accept(ReferencePipeline.java: 197 ) at java.base/java.util.stream.Streams$StreamBuilderImpl.tryAdvance(Streams.java: 397 ) at java.base/java.util.stream.ReferencePipeline.forEachWithCancel(ReferencePipeline.java: 129 ) at java.base/java.util.stream.AbstractPipeline.copyIntoWithCancel(AbstractPipeline.java: 527 ) at java.base/java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java: 513 ) at java.base/java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java: 499 ) at java.base/java.util.stream.FindOps$FindOp.evaluateSequential(FindOps.java: 150 ) at java.base/java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java: 234 ) at java.base/java.util.stream.ReferencePipeline.findFirst(ReferencePipeline.java: 647 ) at in.bench.resources.java8.string.methods.GetACharacterFromString .main(GetACharacterFromString.java: 24 ) |
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 !!