Java 将字符转换为大写

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3696441/
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-14 03:35:48  来源:igfitidea点击:

Converting a char to uppercase

javacharuppercase

提问by shep

String lower = Name.toLowerCase();
int a = Name.indexOf(" ",0);
String first = lower.substring(0, a);
String last = lower.substring(a+1);
char f = first.charAt(0);
char l = last.charAt(0);
System.out.println(l);

how would i get the F and L variables converted to uppercase.

我如何将 F 和 L 变量转换为大写。

回答by DaveJohnston

f = Character.toUpperCase(f);
l = Character.toUpperCase(l);

回答by Andreas Dolk

Have a look at the java.lang.Characterclass, it provides a lot of useful methods to convert or test chars.

看看这个java.lang.Character类,它提供了很多有用的方法来转换或测试字符。

回答by BalusC

You can use Character#toUpperCase()for this.

您可以Character#toUpperCase()为此使用。

char fUpper = Character.toUpperCase(f);
char lUpper = Character.toUpperCase(l);

It has however some limitations since the world is aware of many more characters than can ever fit in 16bit charrange. See also the following excerpt of the javadoc:

然而,它有一些限制,因为全世界都知道比 16 位char范围内所能容纳的字符多得多。另请参阅javadoc的以下摘录:

Note: This method cannot handle supplementary characters. To support all Unicode characters, including supplementary characters, use the toUpperCase(int)method.

注意:此方法无法处理 增补字符。要支持所有 Unicode 字符,包括增补字符,请使用toUpperCase(int)方法。

回答by Roman

The easiest solution for your case - change the first line, let it do just the opposite thing:

最简单的解决方案 - 更改第一行,让它做相反的事情:

String lower = Name.toUpperCase ();

Of course, it's worth to change its name too.

当然,改名也是值得的。

回答by Asaf

If you are including the apache commons lang jar in your project than the easiest solution would be to do:

如果您在项目中包含 apache commons lang jar,那么最简单的解决方案是:

WordUtils.capitalize(Name)

takes care of all the dirty work for you. See the javadoc here

为您处理所有肮脏的工作。请参阅此处的 javadoc

Alternatively, you also have a capitalizeFully(String) method which also lower cases the rest of the characters.

或者,您还有一个 capitalizeFully(String) 方法,它也小写其余字符。

回答by Burrito

Since you know the chars are lower case, you can subtract the according ASCII value to make them uppercase:

由于您知道字符是小写的,您可以减去相应的 ASCII 值使它们大写:

char a = 'a';
a -= 32;
System.out.println("a is " + a); //a is A

Here is an ASCII tablefor reference

这是一个ASCII 表供参考

回答by ???? ????

You can apply the .toUpperCase() directly on String variables or as an attribute to text fields. Ex: -

您可以将 .toUpperCase() 直接应用于字符串变量或作为文本字段的属性。前任: -

String str;
TextView txt;

str.toUpperCase();// will change it to all upper case OR
txt.append(str.toUpperCase());
txt.setText(str.toUpperCase());

回答by Rahul Sharma

Instead of using existing utilities, you may try below conversion using boolean operation:

您可以尝试使用布尔运算进行以下转换,而不是使用现有的实用程序:

To upper case:

大写:

 char upperChar = 'l' & 0x5f

To lower case:

小写:

   char lowerChar = 'L' ^ 0x20


How it works:

这个怎么运作:

Binary, hex and decimal table:

二进制、十六进制和十进制表:

------------------------------------------
| Binary   |   Hexadecimal     | Decimal |
-----------------------------------------
| 1011111  |    0x5f           |  95     |
------------------------------------------
| 100000   |    0x20           |  32     |
------------------------------------------

Let's take an example of small lto Lconversion:

让我们举一个小lL转换的例子:

The binary AND operation: (l & 0x5f)

二元与运算: (l & 0x5f)

lcharacter has ASCII108 and 01101100is binary represenation.

l字符具有ASCII108 并且01101100是二进制表示。

   1101100
&  1011111
-----------
   1001100 = 76 in decimal which is **ASCII** code of L

Similarly the Lto lconversion:

类似的Ltol转换:

The binary XOR operation: (L ^ 0x20)

二元异或运算: (L ^ 0x20)

   1001100
^  0100000
-----------
   1101100 = 108 in decimal which is **ASCII** code of l

回答by Likhith Kumar

System.out.println(first.substring(0,1).toUpperCase()); 
System.out.println(last.substring(0,1).toUpperCase());

回答by Praveen

I think you are trying to capitalize first and last character of each word in a sentence with space as delimiter.

我认为您正在尝试将句子中每个单词的第一个和最后一个字符大写,并以空格作为分隔符。

Can be done through StringBuffer:

可以通过StringBuffer

public static String toFirstLastCharUpperAll(String string){
    StringBuffer sb=new StringBuffer(string);
        for(int i=0;i<sb.length();i++)
            if(i==0 || sb.charAt(i-1)==' ' //for first character of string/each word
                || i==sb.length()-1 || sb.charAt(i+1)==' ') //for last character of string/each word
                sb.setCharAt(i, Character.toUpperCase(sb.charAt(i)));
     return sb.toString();
}