大写或小写单词“Java"中的特定字符 [英] Uppercase or Lowercase a specific character in a word "Java"
本文介绍了大写或小写单词“Java"中的特定字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我已经研究了大部分问题,但我找不到如何在单词中大写或小写特定字符.
I have look into most of questions but I couldn't find how to uppercase or lowercase specific character inside a word.
示例:
String name = "Robert"
如果我想让b"大写和其余小写怎么办?如何使第一个字母大写和其余小写?
What if I would like to make "b" Uppercase and rest lowercase also how to make first letter Uppercase and rest lowercase?
像约翰">>输出>>约翰"...
Like "john" >> Output >> "John"...
我有 toUppercase()
和 toLowercase()
.他们转换整个文本.
I have toUppercase()
and toLowercase()
. They convert the whole text.
我也尝试包含 charAt
但从未与我合作过.
Also I tried to include charAt
but never worked with me.
推荐答案
//Try this...
String str = "Robert";
for (int i = 0; i < str.length(); i++) {
int aChar = str.charAt(i);
// you can directly use character instead of ascii codes
if (aChar == 'b') {
aChar = aChar - 32;
} else if (aChar >= 'A' && aChar <= 'Z') {
aChar += 32 ;
}
System.out.print((char) aChar);
}
/*
输出将是-roBert
/*
Output will be- roBert
*/
这篇关于大写或小写单词“Java"中的特定字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文