在Java中,如何将字符串的所有单词转换为句子?

时间:2010-12-21 18:25:55

标签: java string

在Java中,你如何转换它:

little bo peep HAS LOST HER SHEEP And Doesn't Know WHERE to Find them

到此:

Little Bo Peep Has Lost Her Sheep And Doesn't Know Where To Find Them

5 个答案:

答案 0 :(得分:6)

答案 1 :(得分:1)

public static void main(String[] args) {

  String str = "little bo peep HAS LOST HER SHEEP And Doesn't Know WHERE to Find them";
  str = str.toLowerCase();
  final StringTokenizer st = new StringTokenizer(str, " ");
  final StringBuilder sb = new StringBuilder();

  while (st.hasMoreTokens()) {
   String token = st.nextToken();
   sb.append(Character.toUpperCase(token.charAt(0))
     + token.substring(1) + " ");
  }

  System.out.println(sb.toString().trim());
 }

答案 2 :(得分:1)

或普通拆分版:

public static String capWords(String s) {
    if (s == null && s.isEmpty()) {
        return s;
    } else {
        StringBuilder sb = new StringBuilder();
        for (String token : s.split(" ")) {
            if (token.isEmpty()) {
                if (sb.length() > 0) {
                    sb.append(" ");
                }
            } else {
                if (sb.length() > 0) {
                    sb.append(" ");
                }
                sb.append(Character.toUpperCase(token.charAt(0)));
                if (token.length() > 1) {
                    sb.append(token.substring(1).toLowerCase());
                }
            }
        }
        return sb.toString();
    }
}

注意:此版本还会修剪前导/尾随空格(可能需要也可能不需要)

答案 3 :(得分:0)

    private static String capitalize(String s){
        String str = s.toLowerCase();
        char[] c = str.toCharArray();

        c[0] = Character.toUpperCase(c[0]);
        return new String(c);
    }

    private static String format(String s){
        String[] splitted = s.split(" ");
        StringBuffer sb = new StringBuffer();

        for(int i=0, l = splitted.length; i < l; i++){
            if(splitted[i].trim().length() != 0){
                 sb.append(capitalize(splitted[i]) + " ");
            }

            if(i != l-1){
                 sb.append(" ");
            }
        }

        return sb.toString();
    }


    public static void main(String... args){

        System.out.println(format("little bo peep HAS LOST HER SHEEP And Doesn't Know WHERE to Find them"));

    }

答案 4 :(得分:0)

import java.io。*;

课程ex3

{

public static void main(String arg[])
{
    DataInputStream dis = new DataInputStream(System.in);
    try
    {
        System.out.println("ENTER THE STRING::");
        String s=dis.readLine();
        String res="";
        char fchar=Character.toUpperCase(s.charAt(0));
        res=res+fchar;
        for(int i=1;i<=s.length()-1;i++)
        {
                if(s.charAt(i)==' ')
                res=res+s.charAt(i);
                else if(s.charAt(i)=='.'||s.charAt(i)=='?'||s.charAt(i)=='!')
                {
                    res=res+s.charAt(i);
                    res=res+Character.toUpperCase(s.charAt(i+1));
                    i=i+1;
                }
                else 
                    res=res+Character.toLowerCase(s.charAt(i));
        }
        System.out.println(res);
    }
    catch(Exception e){}
}   

}