0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Salesforce:全角数字を半角数字に変換する

Last updated at Posted at 2022-09-11

Visualforceにて入力された値など、
全角数字を半角数字に変換する必要がある場合に使用するメソッドを作った。

構成

①入力値がNULLの場合はそのままreturn 
 ※呼び出し元でエラーを検知
②入力値がすべて半角数字の場合はそのままreturn
③全角数字(Key)と半角数字(Value)をセットにしたMapを作成
④入力値に全角数字(Key)が含まれていたら半角数字(Value)に変換する
 ※0~9までをループで変換実施
⑤出来上がった文字列をreturn!:kissing_cat: 

実装

sample.cls
private String convertFullNumber(String inputNumber){
        if(String.isEmpty(inputNumber)) return '';
        
        if (Pattern.matches('[0-9]+', inputNumber))  return inputNumber;
        
        Map<String, String> convertNumberMap = new Map<String, String>();
        convertNumberMap.put( '0',  '0' );
        convertNumberMap.put( '1',  '1' );
        convertNumberMap.put( '2',  '2' );
        convertNumberMap.put( '3',  '3' );
        convertNumberMap.put( '4',  '4' );
        convertNumberMap.put( '5',  '5' );
        convertNumberMap.put( '6',  '6' );
        convertNumberMap.put( '7',  '7' );
        convertNumberMap.put( '8',  '8' );
        convertNumberMap.put( '9',  '9' );
        
        for (String full : convertNumberMap.keySet()) {
            if (inputNumber.contains(full)) {
                inputNumber = inputNumber.replaceAll(full, convertNumberMap.get(full));
            }
        }
       
        return inputNumber;
    }


Salesforce関係ない気がするw 
ほぼjava?

まぁただのチートシートなのである。少しでも時短!( ^ω^)

0
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?