In this article, we will understand with a Java program on how to convert a String into Lowercase String in Java 1.8 version
Already in one of the previous article, we discussed how to convert a String into Lowercase String using earlier versions of Java like 5 or 7, etc.
Convert String into Lowercase String :
- toLowerCase() method of String
- Returns lowercase String for the invoking String
- If invoking string is null, then java.lang.NullPointerException is thrown
ConvertStringToLowercase.java
package in.bench.resources.java8.string.methods;
import java.util.stream.Stream;
public class ConvertStringToLowercase {
public static void main(String[] args) {
// 1. string
String url = "BenchResourcesNet";
// 1.1 convert to LowerCase String
String urlLowerCase = Stream.of(url).map(String::toLowerCase).findFirst().get();
System.out.println("Lowercase converted String for (" + url + ") is :- \n"
+ urlLowerCase);
// 2. Empty string
String str1 = "";
// 2.1 convert to LowerCase String
String str1Lower = Stream.of(str1).map(String::toLowerCase).findFirst().get();
System.out.println("\nLowercase converted String for (" + str1 + ") is :- \n"
+ str1Lower + "\n\n");
// 3. null string
String str2 = null;
// 3.1 convert to LowerCase String
String str2Lower = Stream.of(str2).map(String::toLowerCase).findFirst().get();
System.out.println("Lowercase converted String for (" + str2 + ") is :- \n"
+ str2Lower);
}
}
Output:
Lowercase converted String for (BenchResourcesNet) is :-
benchresourcesnet
Lowercase converted String for () is :-
Exception in thread "main" java.lang.NullPointerException
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.ConvertStringToLowercase
.main(ConvertStringToLowercase.java:34)
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 !!