LoginSignup
1
1

More than 5 years have passed since last update.

全角から半角にする

Last updated at Posted at 2017-04-21

こんな感じの雑な実装

/**
 * 全角から半角にする拡張
 * @returns {string}
 */
String.prototype.toHankaku = function () {
    var h = '';
    this.split('').forEach(function (s) {
        if( 0xFEE0 <= s.charCodeAt(0) ){
            h += String.fromCharCode(s.charCodeAt(0) - 0xFEE0);
        } else {
            h += s;
        }
    });
    return h;
};


var han = "ABCDEFG".toHankaku();
console.log(han);
1
1
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
1
1