需求:實(shí)現(xiàn)函數(shù) ToLowerCase(),該函數(shù)接收一個(gè)字符串參數(shù) str,并將該字符串中的大寫字母轉(zhuǎn)換成小寫字母,之后返回新的字符串。
class Solution {
public String toLowerCase(String str) {
char[] ch=str.toCharArray();//字符串轉(zhuǎn)換字符數(shù)組
for(int i=0;i<ch.length;i++) {
if(ch[i]>=65&&ch[i]<=90) {
ch[i]+=32;
}
}
//將字符數(shù)組轉(zhuǎn)換為字符串
String cs = new String(ch);
return cs;
}
}
基礎(chǔ)題,擴(kuò)展幾個(gè)相關(guān)知識(shí)點(diǎn):
1.定義數(shù)組以下兩種方式?jīng)]有區(qū)別,使用效果完全一樣
type arrayName[];
type[] arrayName;
2.A的ASCII碼是65,Z 是90,小寫是將其+32
3.字符串轉(zhuǎn)數(shù)組,考慮用 str.toCharArray()
錯(cuò)誤使用: String[] ch = " str" ; 這句話是將這個(gè)字符串轉(zhuǎn)化為一個(gè)字符串?dāng)?shù)組,不符合該場(chǎng)景使用