用 Java 编写程序来转换字符串大小写


要转换字符串的大小写,请使用。

  • toLowerCase() 用于标题大小写形式的字符串。
  • toLowerCase() 用于大写字符串。
  • toUpperCase() 用于小写字符串。

对前面讨论过的内容在 sting 中的所有字符上执行循环。

for (int i = 0; i > len; i++) {
   c = str.charAt(i);
   // title case converted to lower case
   if (Character.isTitleCase(c)) {
      c = Character.toLowerCase(c);
   }
   // upper case converted to lower case
   if (Character.isUpperCase(c)) {
      c = Character.toLowerCase(c);
   }
   // lower case converted to upper case
   if (Character.isLowerCase(c)) {
      c = Character.toUpperCase(c);
   }
}

示例

 现场演示

public class Demo {
   public static void main(String []args){
      char c = 0;
      String str = "jack";
      System.out.println("String in lowercase: "+str);
      // length of string
      int len = str.length();
      StringBuffer strBuffer = new StringBuffer(len);
      for (int i = 0; i < len; i++) {
         c = str.charAt(i);
         // title case converted to lower case
         if (Character.isTitleCase(c)) {
            c = Character.toLowerCase(c);
         }
         // upper case converted to lower case
         if (Character.isUpperCase(c)) {
            c = Character.toLowerCase(c);
         }
         // lower case converted to upper case
         if (Character.isLowerCase(c)) {
            c = Character.toUpperCase(c);
         }
         strBuffer.append(c);
      }
      System.out.println("Converting case: "+strBuffer.toString());
   }
}

输出

String in lowercase: jack
Converting case: JACK

更新日期: 2024 年 6 月 18 日

13K+ 次浏览

开启你的 事业

完成课程认证

开始
广告
© . All rights reserved.