Capitalize each word in a sentence with loop in Java
Description
The following code shows how to capitalize each word in a sentence with loop.
Example
/*from ww w. j ava2 s . c o m*/
public class Main {
public static void main(String[] a) {
String sentence = "this is a test from java2s.com";
String tmp = "";
String[] words = sentence.split(" ");
for (int i = 0; i < words.length; i++) {
tmp += words[i].substring(0, 1).toUpperCase();
tmp += words[i].substring(1).toLowerCase();
tmp += " ";
}
System.out.println(tmp.trim());
}
}
The code above generates the following result.
Home »
Java Tutorial »
Java Data Type »
Java Tutorial »
Java Data Type »