一、字符串转化为大写。把所有字符转化为大写,只要遍历所有字符,转化为大写就行。
package wordupper;
public class StringUpper {
public static void main(String[] args) {
String s = "tomorrow will be better!";
System.out.println("原句子:" + s);
System.out.print("转化后:");
changed(s);
}
private static void changed(String s) {
// TODO Auto-generated method stub
for(int i = 0; i < s.length(); i++){
System.out.print(Character.toUpperCase(s.charAt(i)));
}
}
}
二、单词首字母转化为大写。关键是要找出单词首字母
<一>原理同转化字符,遍历字符,找到空格,空格后一位就是单词首字母。此方法不能在句子末尾再加空格
package wordupper;
public class WordsUpper1 {
public static void main(String[] args)
{
String str = new String("tomorrow will be better!");
System.out.println("原句子:" + str);
System.out.println("转化后:" + changed(str));
}
private static String changed(String str) {
// TODO Auto-generated method stub
char[] c = new char[str.length()]

本文介绍如何将字符串转化为大写以及实现单词首字母大写的技巧。在字符串转化为大写的方法中,只需遍历字符并将其转化为大写。而对于单词首字母大写,可以通过寻找空格来确定单词边界,空格后的第一个字符即为单词首字母,但注意不要在句子末尾额外添加空格。
最低0.47元/天 解锁文章
2492

被折叠的 条评论
为什么被折叠?



