Java - Convert the first character of each word in a string to Lowercase
In this section, we will show five different ways to convert the first character of each word in a string to Lowercase in Java.
1. Using For loop and toLowerCase() method
2. Using Java 8 Streams
3. Using regex and replaceAll() method
4. Using Apache Commons Text
5. Using Google Guava
1. Using For loop and toLowerCase() method
public class Main {
public static void main(String[] args) {
String string = "JAVA IS AWESOME";
String[]arr = string.split(" ");
StringBuilder stringBuilder = new StringBuilder ();
for(int i=0; i<arr.length;i++)
{
stringBuilder.append(Character.toLowerCase(arr[i].charAt(0)))
.append(arr[i].substring(1)).append(" ");
}
String result = stringBuilder.toString();
System.out.println(result);
}
}
Console Output:
jAVA iS aWESOME
jAVA iS aWESOME
2. Using Java 8 Streams
import java.util.Arrays;
import java.util.stream.Collectors;
public class Main {
public static void main(String[] args) {
String string = "JAVA IS AWESOME";
String collect = Arrays.stream(string.split(" "))
.map(o -> o.substring(0, 1).toLowerCase() + o.substring(1))
.collect(Collectors.joining(" "));
System.out.println(collect);
}
}
Here split() method in Java splits a string into substrings using a delimiter that is specified using a regular expression. Here delimiter is whitespace. It returns an array of String.
stream() method accepts a mandatory parameter array which is the array of whose elements are to be converted into a sequential stream and returns a Sequential Stream from the array passed as the parameter.
map() operation transforms the elements of a stream from one type to another. Here we convert the first letter of each word in a string to Lowercase.
Java Stream collect() performs a mutable reduction operation on the elements of the stream. This is a terminal operation.
joining() returns a Collector that concatenates the input String with the whitespace.
Console Output:
jAVA iS aWESOME
jAVA iS aWESOME
3. Using regex and replaceAll() method
import java.util.regex.Pattern;
public class Main {
public static void main(String[] args) {
String string = "JAVA IS AWESOME";
String result = Pattern.compile("\\b(.)(.*?)\\b")
.matcher(string)
.replaceAll(match -> match.group(1)
.toLowerCase()+match.group(2));
System.out.println(result);
}
}
compile() method is utilized to engender a pattern from the regular expression passed as parameter to method.
matcher() method is utilized to probe for our concrete pattern in a string. It returns a Matcher object which contains information about the probe that was performed.
replaceAll() method reads the input string and replace it with the matched pattern in the matcher string.
Console Output:
jAVA iS aWESOME
jAVA iS aWESOME
4. Using Apache Commons Text
import org.apache.commons.text.WordUtils;
public class Main {
public static void main(String[] args) {
String string = "JAVA IS AWESOME";
String result = WordUtils.uncapitalize(string);
System.out.println(result);
}
}
uncapitalize() method uncapitalize the first character of each word in a given string
Console Output:
jAVA iS aWESOME
jAVA iS aWESOME
5. Using Google Guava
import com.google.common.base.Function;
import com.google.common.base.Joiner;
import com.google.common.base.Splitter;
import com.google.common.collect.Iterables;
public class Main {
public static void main(String[] args) {
String string = "JAVA IS AWESOME";
String result = Joiner.on(' ').join(Iterables.transform(Splitter.on(' ')
.omitEmptyStrings().split(string), new Function<String, String>() {
public String apply(String input) {
return Character.toLowerCase(input.charAt(0))
+ input.substring(1);
}
}));
System.out.println(result);
}
}
Console Output:
jAVA iS aWESOME
jAVA iS aWESOME
More Interesting topics,