文字列内の大文字を小文字に変換する

アイデア

  • 新しい文字列変数の結果を作成して、変換後の結果を保存します
  • 変換する文字列の各文字を取り出します(str.charAt(i))
  • 大文字の場合は、小文字と大文字の差( 'a'-'A')を追加すると、最終的な変換結果が結果にスプライスされます
  • 大文字でない場合は、変換を行わず、直接結果に接続します
  • 結果を返す

コード

public class Pra0117 {
    public static void main(String[] args) {
        String str1="HELLOapple0117";
        System.out.println(toLower(str1));
    }

    public static String toLower(String str) {
        String result="";
        for(int i=0;i<str.length();i++){
            char pos=str.charAt(i);
            if('A'<=pos&&(pos<='Z')) {
                result += (char) (pos+ ('a' - 'A'));//注意这里要将转换结果强转为char类型
            }else{
                result+=pos;
            }
        }
        return result;
    }

}

運転結果

~~~~~サプリメント~~~~~~~

前の演習では気づかなかった小さな詳細を見つけました。パブリックに変更されたクラス名の記述仕様は、できるだけ大きくする必要があります~~

おすすめ

転載: blog.csdn.net/weixin_43939602/article/details/112753182