简体   繁体   English

如何在Java中在大写和小写之间转换字符串?

[英]How do I convert strings between uppercase and lowercase in Java?

Java中字符串大小写转换的方法是什么?

String#toLowerCaseString#toUpperCase是您需要的方法。

There are methods in the String class; String类中有方法; toUppercase() and toLowerCase() . toUppercase()toLowerCase()

ie

String input = "Cricket!";
String upper = input.toUpperCase(); //stores "CRICKET!"
String lower = input.toLowerCase(); //stores "cricket!" 

This will clarify your doubt这将澄清你的疑惑

Yes.是的。 There are methods on the String itself for this.为此,String 本身有一些方法。

Note that the result depends on the Locale the JVM is using.请注意,结果取决于 JVM 使用的区域设置。 Beware, locales is an art in itself.请注意,语言环境本身就是一门艺术。

Assuming that all characters are alphabetic, you can do this:假设所有字符都是字母,你可以这样做:

From lowercase to uppercase:从小写到大写:

// Uppercase letters. 
class UpperCase {  
  public static void main(String args[]) { 
    char ch;
    for(int i=0; i < 10; i++) { 
      ch = (char) ('a' + i);
      System.out.print(ch); 

      // This statement turns off the 6th bit.   
      ch = (char) ((int) ch & 65503); // ch is now uppercase
      System.out.print(ch + " ");  
    } 
  } 
}

From uppercase to lowercase:从大写到小写:

// Lowercase letters. 
class LowerCase {  
  public static void main(String args[]) { 
    char ch;
    for(int i=0; i < 10; i++) { 
      ch = (char) ('A' + i);
      System.out.print(ch);
      ch = (char) ((int) ch | 32); // ch is now uppercase
      System.out.print(ch + " ");  
    } 
  } 
}

Coverting the first letter of word capital覆盖单词大写的第一个字母

input:输入:

hello world你好世界

String A = hello;
String B = world;
System.out.println(A.toUpperCase().charAt(0)+A.substring(1) + " " + B.toUpperCase().charAt(0)+B.substring(1));

Output:输出:

Hello World你好世界

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM