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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1951856/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 01:44:37  来源:igfitidea点击:

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

javastring

提问by Nanda

What is the method for converting strings in Java between upper and lower case?

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

采纳答案by PeterMmm

String#toLowerCaseand String#toUpperCaseare the methods you need.

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

回答by Thorbj?rn Ravn Andersen

Yes. There are methods on the String itself for this.

是的。为此,String 本身有一些方法。

Note that the result depends on the Locale the JVM is using. Beware, locales is an art in itself.

请注意,结果取决于 JVM 使用的区域设置。请注意,语言环境本身就是一门艺术。

回答by Crickey

There are methods in the String class; toUppercase()and toLowerCase().

String类中有方法;toUppercase()toLowerCase()

i.e.

IE

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

This will clarify your doubt

这将澄清你的疑惑

回答by zubin patel

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

你好,世界

回答by Jaime Montoya

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 + " ");  
    } 
  } 
}