有没有内置的方法可以将一个字符串转换为标题格式?
System.out.println(WordUtils.capitalizeFully("tHis will BE MY EXAMple"));
将会产生这个
这将是我的例子
我不确定为什么它被命名为“capitalizeFully”,实际上该函数并没有完全大写结果,但无论如何,这就是我们需要的工具。
capitalizeFully
,因为它将每个单词都大写,包括标题中应该是小写的单词。http://grammar.about.com/od/tz/g/Title-Case.htm - aij抱歉,我是个初学者,所以我的编程习惯很糟糕!
public class TitleCase {
String title(String sent)
{
sent =sent.trim();
sent = sent.toLowerCase();
String[] str1=new String[sent.length()];
for(int k=0;k<=str1.length-1;k++){
str1[k]=sent.charAt(k)+"";
}
for(int i=0;i<=sent.length()-1;i++){
if(i==0){
String s= sent.charAt(i)+"";
str1[i]=s.toUpperCase();
}
if(str1[i].equals(" ")){
String s= sent.charAt(i+1)+"";
str1[i+1]=s.toUpperCase();
}
System.out.print(str1[i]);
}
return "";
}
public static void main(String[] args) {
TitleCase a = new TitleCase();
System.out.println(a.title(" enter your Statement!"));
}
}