在Java中将字符转换为大写

发布于 2024-09-18 19:07:50 字数 290 浏览 5 评论 0原文

如何将 fl 变量转换为大写?

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(11

你的背包 2024-09-25 19:08:14

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

String lower = Name.toUpperCase ();

当然,也值得更改其名称。

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.

谈下烟灰 2024-09-25 19:08:11

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

可以通过 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();
}

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:

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();
}
夏天碎花小短裙 2024-09-25 19:08:08

假设您有一个想要拆分的变量

String name = "Your name variable";
char nameChar = Character.toUpperCase(name.charAt(0));

Lets assume you have a variable you want split

String name = "Your name variable";
char nameChar = Character.toUpperCase(name.charAt(0));
黯淡〆 2024-09-25 19:08:06

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

String str;
TextView txt;

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

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

String str;
TextView txt;

str.toUpperCase();// will change it to all upper case OR
txt.append(str.toUpperCase());
txt.setText(str.toUpperCase());
草莓酥 2024-09-25 19:08:04

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

WordUtils.capitalize(Name)

为您处理所有脏活。
请参阅 javadoc 这里

或者,您还可以使用 CapitalizeFully(String) 方法,该方法也会将其余字符小写。

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

WordUtils.capitalize(Name)

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.

梦毁影碎の 2024-09-25 19:08:03
System.out.println(first.substring(0,1).toUpperCase()); 
System.out.println(last.substring(0,1).toUpperCase());
System.out.println(first.substring(0,1).toUpperCase()); 
System.out.println(last.substring(0,1).toUpperCase());
离线来电— 2024-09-25 19:08:01

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

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

这是一个 ASCII 表供参考

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

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

Here is an ASCII table for reference

美煞众生 2024-09-25 19:08:00
f = Character.toUpperCase(f);
l = Character.toUpperCase(l);
f = Character.toUpperCase(f);
l = Character.toUpperCase(l);
腹黑女流氓 2024-09-25 19:07:59

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

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

手长情犹 2024-09-25 19:07:57

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

转大写:

 char upperChar = 'l' & 0x5f

转小写:

   char lowerChar = 'L' ^ 0x20

工作原理:

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

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

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

二进制AND运算:(l & 0x5f)

l 字符的 ASCII 108 和 01101100 是二进制表示。

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

Ll转换类似:

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

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

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 l to L conversion:

The binary AND operation: (l & 0x5f)

l character has ASCII 108 and 01101100 is binary represenation.

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

Similarly the L to l conversion:

The binary XOR operation: (L ^ 0x20)

   1001100
^  0100000
-----------
   1101100 = 108 in decimal which is **ASCII** code of l
彼岸花似海 2024-09-25 19:07:56

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

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

然而它也有一些局限性,因为人们知道的字符数量远远超出了 16 位 char 范围所能容纳的字符数量。另请参阅 的以下摘录javadoc

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

You can use Character#toUpperCase() for this.

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 char range. See also the following excerpt of the javadoc:

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文