欢迎使用CSDN-markdown编辑器

来源:互联网 发布:k-means算法 编辑:程序博客网 时间:2024/06/16 08:58

将一个字符串中的单词的开头字母大小 ; example : this is a test of java ; 下面给出两种方法,都比较简单,因为第一次写博客 ,尝试一下;

public static void test(){        String str ="this is a test of java";        String string[] = str.split(" ");        for (int i = 0; i < string.length; i++) {            // 把数组中每一个字符串的第一位截取出来            String string2 = string[i].substring(0, 1);            // 把这一位去大写            String string3 = string2.toUpperCase();            // 将数组中每一个字符串除了第一位截取出来            String string4 = string[i].substring(1);            // 转成大写的第一位和 除了第一位的这个字符串拼接            string3+=string4;            // 首字母大写的单词  赋值给string数组            string[i] = string3;            System.out.println(string[i]);        }    }
public static  void  up(String str){        char  ch [] =str.toCharArray();        ch[0]=((char)(ch[0]-32));        for (int i = 0; i < ch.length; i++) {            //System.out.println(ch[i]);            if (ch[i]==' ') {                 ch[i+1]=(char)(ch[i+1]-32);                            }        }        for (char c : ch) {            System.out.print(c);        }        System.out.println();    }
0 0
原创粉丝点击