Converting a lowercase char in a char array to an uppercase char (java)

对着背影说爱祢 提交于 2019-12-12 18:06:01

问题


Hello I am trying to write a little segment of code that checks if each char in a char array is lower case or uppercase. Right now it uses the char's ASCII number to check. After it checks it should convert the char to upper case if it is not already so:

for (int counter = 0; counter < charmessage.length; counter++) {
    if (91 - charmessage[counter] <= 0 && 160 - charmessage[counter] != 0) {
    charmessage[counter] = charmessage[counter].toUpperCase();
    } 
}

charmessage is already initialized previously in the program. The 160 part is to make sure it doesn't convert a space to uppercase. How do I get the .toUpperCase method to work?


回答1:


I would do it this way. First check if the character is a letter and if it is lowercase. After this just use the Character.toUpperCase(char ch)

if(Character.isLetter(charmessage[counter]) && Character.isLowerCase(charmessage[counter])){
    charmessage[counter] = Character.toUpperCase(charmessage[counter]);
}



回答2:


You can use the Character#toUpperCase for that. Example:

char a = 'a';
char upperCase = Character.toUpperCase(a);

It has some limitations, though. It's very important you know that the world is aware of many more characters that can fit within the 16-bit range.



来源:https://stackoverflow.com/questions/16635264/converting-a-lowercase-char-in-a-char-array-to-an-uppercase-char-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!