5 Ways: Java Program to Capitalize the first character of each word in a string
In this section, we will show five different ways to capitalize the first letter of each word in a string in Java.
1. Using For loop and toUpperCase() 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 toUpperCase() 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.toUpperCase(arr[i].charAt(0)))
.append(arr[i].substring(1)).append(" ");
}
String result = stringBuilder.toString();
System.out.println(result);
}
}
Here we are using split() split the string based on whitespace. It returns an Array of String.
Iterate over String Array using For loop. While iterating we capitalize first character of each word using toUpperCase() method. We are using StringBuilder for concatenation. StringBuilder doesn't need to re-create the String object over and over.
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).toUpperCase() + 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 capitalize the first character of each word and put in the new Stream.
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)
.toUpperCase()+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.capitalize(string);
System.out.println(result);
}
}
capitalize() method capitalize 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.toUpperCase(input.charAt(0))
+ input.substring(1);
}
}));
System.out.println(result);
}
}
Console Output:
Java Is Awesome
Java Is Awesome
More Interesting topics,