0

我在这个论坛上搜索了一下,我似乎只能找到关于如何将每个单词的第一个字母大写的问题。这不是我要找的。

我正在寻找可以检查字符串中所有单词的东西,如果它们是大写的,则会将字母更改为小写,除了第一个。

就像,假设字符串是:

“HI 堆栈溢出”

它会将其更改为:

“嗨 Stackoverflow”

或者:

“我在 stackoverflow dot com 上提问”

它会将其更改为:

“我在 stackoverflow dot com 上提问”

4

3 回答 3

4

I would use the StringTokenizer class to break the string up into the separate words. Then you can get each token as a separate String and compare:

String line = "A BIG Thing that Something"

StringTokenizer st = new StringTokenizer(line);

        while(st.hasMoreTokens)
            {
                String a = st.nextToken();
                if(a.equals(a.toUpperCase())){
                    System.out.println(a.charAt(0) + a.substring(1).toLowerCase());
                }else{
                    System.out.println(a);
                    }
            }

Something like that... You'll need to remember to import StringTokenizer, it's part of the java.util package.

于 2012-04-23T04:13:20.053 回答
1
   public static void main(String[] args) {
      String org= "HI STACKOVERFLOW";
      String [] temp=org.split(" ");
      int len=temp.length;
      String ne = ".";
      for(int i=0;i<len;i++)
      {

         temp[i]=temp[i].toUpperCase();
         temp[i]=(temp[i].substring(0, 1)).toUpperCase()+(temp[i].substring(1, temp[i].length())).toLowerCase();
        System.out.print(temp[i]+" "); 
      }

    }

输出嗨 Stackoverflow

于 2013-12-18T12:54:22.413 回答
-1

If you're open to incorporate a library in your project, I'm quite sure that Apache Commons-lang StringUtils has the type of functionality you need.

于 2012-04-23T04:14:37.843 回答