【Kotlin】半角から全角へ置換する

2020年7月4日Kotlin

スポンサーリンク

説明

半角から全角へ置換する

 

引数

引数1(target:String):置換対象の文字列

 

返り値

置換後の文字列

 

コード

/*
=========================================================
[概  要] 半角から全角へ置換する
[引  数] target        : 置換対象の文字列
[戻り値] 置換後の文字列
[備  考]
=========================================================
*/
fun convertIntoFullFromHalf(target : String ):String
{
    var str : String

    //アルファベット(大文字)の変換
    str = target.replace("A","A" ).replace("B","B" ).replace("C","C" ).replace("D","D" ).replace("E","E" ).replace("F","F" ).replace("G","G" ).replace("H","H" ).replace("I","I" ).replace("J","J" ).replace("K","K" ).replace("L","L" ).replace("M","M" ).replace("N","N" ).replace("O","O" ).replace("P","P" ).replace("Q","Q" ).replace("R","R" ).replace("S","S" ).replace("T","T" ).replace("U","U" ).replace("V","V" ).replace("W","W" ).replace("X","X" ).replace("Y","Y" ).replace("Z","Z" )
    //アルファベット(小文字)の変換
    str = str.replace("a","a" ).replace("b","b" ).replace("c","c" ).replace("d","d" ).replace("e","e" ).replace("f","f" ).replace("g","g" ).replace("h","h" ).replace("i","i" ).replace("j","j" ).replace("k","k" ).replace("l","l" ).replace("m","m" ).replace("n","n" ).replace("o","o" ).replace("p","p" ).replace("q","q" ).replace("r","r" ).replace("s","s" ).replace("t","t" ).replace("u","u" ).replace("v","v" ).replace("w","w" ).replace("x","x" ).replace("y","y" ).replace("z","z" )
    //数値の変換
    str = str.replace("0","0" ).replace("1","1" ).replace("2","2" ).replace("3","3" ).replace("4","4" ).replace("5","5" ).replace("6","6" ).replace("7","7" ).replace("8","8" ).replace("9","9" )
    //カタカナの変換
    str = str.replace("ア","ア" ).replace("イ","イ" ).replace("ウ","ウ" ).replace("エ","エ" ).replace("オ","オ" ).replace("カ","カ" ).replace("キ","キ" ).replace("ク","ク" ).replace("ケ","ケ" ).replace("コ","コ" ).replace("サ","サ" ).replace("シ","シ" ).replace("ス","ス" ).replace("セ","セ" ).replace("ソ","ソ" ).replace("タ","タ" ).replace("チ","チ" ).replace("ツ","ツ" ).replace("テ","テ" ).replace("ト","ト" ).replace("ナ","ナ" ).replace("ニ","ニ" ).replace("ヌ","ヌ" ).replace("ネ","ネ" ).replace("ノ","ノ" ).replace("ハ","ハ" ).replace("ヒ","ヒ" ).replace("フ","フ" ).replace("ヘ","ヘ" ).replace("ホ","ホ" ).replace("マ","マ" ).replace("ミ","ミ" ).replace("ム","ム" ).replace("メ","メ" ).replace("モ","モ" ).replace("ヤ","ヤ" ).replace("ユ","ユ" ).replace("ヨ","ヨ" ).replace("ラ","ラ" ).replace("リ","リ" ).replace("ル","ル" ).replace("レ","レ" ).replace("ロ","ロ" ).replace("ワ","ワ" ).replace("ヲ","ヲ" ).replace("ン","ン" ).replace("ァ","ァ" ).replace("ィ","ィ" ).replace("ゥ","ゥ" ).replace("ェ","ェ" ).replace("ォ","ォ" ).replace("カ","ヵ" ).replace("ケ","ヶ" ).replace("ッ","ッ" ).replace("ャ","ャ" ).replace("ュ","ュ" ).replace("ョ","ョ" ).replace("ワ","ヮ" ).replace("ヴ","ヴ" ).replace("ガ","ガ" ).replace("ギ","ギ" ).replace("グ","グ" ).replace("ゲ","ゲ" ).replace("ゴ","ゴ" ).replace("ザ","ザ" ).replace("ジ","ジ" ).replace("ズ","ズ" ).replace("ゼ","ゼ" ).replace("ゾ","ゾ" ).replace("ダ","ダ" ).replace("ヂ","ヂ" ).replace("ヅ","ヅ" ).replace("デ","デ" ).replace("ド","ド" ).replace("バ","バ" ).replace("ビ","ビ" ).replace("ブ","ブ" ).replace("ベ","ベ" ).replace("ボ","ボ" ).replace("パ","パ" ).replace("ピ","ピ" ).replace("プ","プ" ).replace("ペ","ペ" ).replace("ポ","ポ" )
    //記号の変換
    str = str.replace("!","!" ).replace("#","#" ).replace("$","$" ).replace("%","%" ).replace("&","&" ).replace("(","(" ).replace(")",")" ).replace("ー","ー" ).replace("-","-" ).replace("=","=" ).replace("^","^" ).replace("~","~" ).replace("|","|" ).replace("@","@" ).replace("`","‘" ).replace("「","「" ).replace("[","[" ).replace("{","{" ).replace("+","+" ).replace(":",":" ).replace("*","*" ).replace("」","」" ).replace("]","]" ).replace("}","}" ).replace("、","、" ).replace(",","," ).replace("<","<" ).replace("。","。" ).replace(".","." ).replace(">",">" ).replace("・","・" ).replace("/","/" ).replace("?","?" ).replace("_","_" )

    return str 
}

 

実行例

println(convertIntoFullFromHalf("ABCDEfghiアイウ123!#$%"))  --ABCDEfghiアイウ123!#$%