I have a String test="The Mountain view"
, i need all character after a space in a String need to be in Upper case , for example in the above text 'M'
is uppercase after space, the condition need to be reflect for every character after space in String.
I need a regular expression or condition to check all character after space is in Upper case or else i need change the String after space into upper case character if it is in lower cas开发者_JAVA百科e.
If anyone knows means help me out.
Thanks.
There's no need for regular expressions. Try this example, maybe helps:
public class Capitalize {
public static String capitalize(String s) {
if (s.length() == 0) return s;
return s.substring(0, 1).toUpperCase() + s.substring(1).toLowerCase();
}
public static void main(String[] args) {
while (!StdIn.isEmpty()) {
String line = StdIn.readLine();
String[] words = line.split("\\s");
for (String s : words) {
StdOut.print(capitalize(s) + " ");
}
StdOut.println();
}
}
}
精彩评论