データと保存
数字で保存し、区切って表示する
normalizenational
保存した数字を、番号表の区切りで表示します。
試す
0352538111、0942210000、0570123456 も試してください。
04998-2-3111
しくみ
多くのシステムは、番号を数字だけで保存します。表示するときは、数字を normalize に渡して national を使います。
ハイフンの位置は、桁数を数えても決まりません。0499823111 は 04998-2-3111、0942210000 は 0942-21-0000 です。市外局番の長さは番号ごとに違います。japan-phone は、総務省の表からそれを読みます。
各言語で
import { normalize } from "@kurashina-design/japan-phone";
// Store only the digits, and add the hyphens when you show the number.
// The groups come from the numbering table: you cannot place them by counting digits.
for (const digits of ["0352538111", "0499823111", "0942210000", "0570123456", "0120950178"]) {
console.log(digits, "->", normalize(digits).national);
}
from japan_phone import normalize
# Store only the digits, and add the hyphens when you show the number.
# The groups come from the numbering table: you cannot place them by counting digits.
for digits in ["0352538111", "0499823111", "0942210000", "0570123456", "0120950178"]:
print(digits, "->", normalize(digits).national)
import jp.kurashina.japanphone.JapanPhoneNumber;
import jp.kurashina.japanphone.JapanPhone;
// Store only the digits, and add the hyphens when you show the number.
// The groups come from the numbering table: you cannot place them by counting digits.
for (String digits : new String[] {"0352538111", "0499823111", "0942210000", "0570123456", "0120950178"}) {
System.out.println(digits + " -> " + JapanPhone.normalize(digits).get().getNational());
}
出力:
0352538111 -> 03-5253-8111
0499823111 -> 04998-2-3111
0942210000 -> 0942-21-0000
0570123456 -> 0570-123-456
0120950178 -> 0120-950-178