Last active
August 29, 2015 14:16
-
-
Save hrdaya/81675e6b63efdc840309 to your computer and use it in GitHub Desktop.
文字列の左側を任意の文字で任意の桁数に埋める(2種類) ref: http://qiita.com/hrdaya/items/ec459928737d66b28015
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 任意の桁数に切り取った文字列を返す | |
* 桁数に足りない場合は任意の文字で左側を埋める | |
* | |
* @param {String} val 元の文字 | |
* @param {Number} num 桁数 | |
* @param {String} pad 文字埋めに使用する文字列 | |
* @return {String} 作成した文字列 | |
*/ | |
var padSlice = function(val, num, pad) { | |
// 桁数が指定されていない時は2桁 | |
num = num === undefined ? 2 : parseInt(num, 10); | |
// 文字埋めに使用する文字列が指定されていない場合は"0" | |
pad = pad === undefined ? "0" : pad.toString(); | |
// 要素数numの配列を区切り文字padで結合しvalと結合 | |
// 出来た文字列の右側からnum文字を切り出す | |
return (Array(num).join(pad) + val).slice(num * -1); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 任意の桁数で任意の文字を使用して左側を埋める | |
* 桁数を超えている場合はそのままの文字列を返す | |
* | |
* @param {String} val 元の文字 | |
* @param {Number} num 桁数 | |
* @param {String} pad 文字埋めに使用する文字列 | |
* @return {String} 作成した文字 | |
*/ | |
var padLoop = function(val, num, pad) { | |
// 桁数が指定されていない時は2桁 | |
num = num === undefined ? 2 : parseInt(num, 10); | |
// 文字埋めに使用する文字列が指定されていない場合は"0" | |
pad = pad === undefined ? "0" : pad.toString(); | |
// valを文字列に変換 | |
val = val.toString(); | |
// 桁数に達するまで左に任意の文字を足す | |
while (val.length < num) { | |
val = pad + val; | |
} | |
return val; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 任意の桁数に切り取った文字列を返す | |
* 桁数に足りない場合は任意の文字で左側を埋める | |
* | |
* @param {Number} num 桁数 | |
* @param {String} pad 文字埋めに使用する文字列 | |
* @return {String} 作成した文字列 | |
*/ | |
String.prototype.padSlice = function(num, pad) { | |
// 桁数が指定されていない時は2桁 | |
num = num === undefined ? 2 : parseInt(num, 10); | |
// 文字埋めに使用する文字列が指定されていない場合は"0" | |
pad = pad === undefined ? "0" : pad.toString(); | |
// 要素数numの配列を区切り文字padで結合しvalと結合 | |
// 出来た文字列の右側からnum文字を切り出す | |
return (Array(num).join(pad) + this).slice(num * -1); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 任意の桁数で任意の文字を使用して左側を埋める | |
* 桁数を超えている場合はそのままの文字列を返す | |
* | |
* @param {Number} num 桁数 | |
* @param {String} pad 文字埋めに使用する文字列 | |
* @return {String} 作成した文字 | |
*/ | |
String.prototype.padLoop = function(num, pad) { | |
// 桁数が指定されていない時は2桁 | |
num = num === undefined ? 2 : parseInt(num, 10); | |
// 文字埋めに使用する文字列が指定されていない場合は"0" | |
pad = pad === undefined ? "0" : pad.toString(); | |
// 値を書き換えてしまわないようにする | |
var val = this; | |
// 桁数に達するまで左に任意の文字を足す | |
while (val.length < num) { | |
val = pad + val; | |
} | |
return val; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment