在Java中将字符转换为大写
如何将 f
和 l
变量转换为大写?
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 could I get the f
and l
variables converted to uppercase?
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);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
对于您的情况,最简单的解决方案 - 更改第一行,让它做相反的事情:
当然,也值得更改其名称。
The easiest solution for your case - change the first line, let it do just the opposite thing:
Of course, it's worth to change its name too.
我认为您正在尝试将句子中每个单词的第一个和最后一个字符大写,并以空格作为分隔符。
可以通过
StringBuffer
完成: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
:假设您有一个想要拆分的变量
Lets assume you have a variable you want split
您可以将 .toUpperCase() 直接应用于字符串变量或作为文本字段的属性。前任: -
You can apply the .toUpperCase() directly on String variables or as an attribute to text fields. Ex: -
如果您在项目中包含 apache commons lang jar,那么最简单的解决方案就是:
为您处理所有脏活。
请参阅 javadoc 这里
或者,您还可以使用 CapitalizeFully(String) 方法,该方法也会将其余字符小写。
If you are including the apache commons lang jar in your project than the easiest solution would be to do:
takes care of all the dirty work for you.
See the javadoc here
Alternatively, you also have a capitalizeFully(String) method which also lower cases the rest of the characters.
由于您知道字符是小写的,因此您可以减去相应的 ASCII 值以使它们变为大写:
这是一个 ASCII 表供参考
Since you know the chars are lower case, you can subtract the according ASCII value to make them uppercase:
Here is an ASCII table for reference
看一下
java.lang.Character
类,它提供了很多有用的方法来转换或测试字符。Have a look at the
java.lang.Character
class, it provides a lot of useful methods to convert or test chars.您可以尝试使用布尔运算进行以下转换,而不是使用现有的实用程序:
转大写:
转小写:
工作原理:
二进制,十六进制和十进制表:
让我们举一个小
l
到L
转换的例子:二进制AND运算:
(l & 0x5f)
l
字符的 ASCII 108 和01101100
是二进制表示。与
L
到l
转换类似:二进制异或运算:
(L ^ 0x20)
Instead of using existing utilities, you may try below conversion using boolean operation:
To upper case:
To lower case:
How it works:
Binary, hex and decimal table:
Let's take an example of small
l
toL
conversion:The binary AND operation:
(l & 0x5f)
l
character has ASCII 108 and01101100
is binary represenation.Similarly the
L
tol
conversion:The binary XOR operation:
(L ^ 0x20)
您可以使用
字符#toUpperCase()
为此。然而它也有一些局限性,因为人们知道的字符数量远远超出了 16 位
char
范围所能容纳的字符数量。另请参阅 的以下摘录javadoc:You can use
Character#toUpperCase()
for this.It has however some limitations since the world is aware of many more characters than can ever fit in 16bit
char
range. See also the following excerpt of the javadoc: