📜  从国际电话号码获取国家 - Javascript代码示例

📅  最后修改于: 2022-03-11 15:02:31.211000             🧑  作者: Mango

代码示例1
/// 
/// Returns the Country based on an international dialing code.
/// 
public static Country? GetCountry(ReadOnlySpan phoneNumber) {
  if (phoneNumber.Length==0) return null;

  var isFirstDigit = true;
  DigitInfo? digitInfo = null;
  Country? country = null;
  foreach (var digitChar in phoneNumber) {
    var digitIndex = digitChar - '0';
    if (isFirstDigit) {
      isFirstDigit = false;
      digitInfo = ByPhone[digitIndex];
    } else {
      if (digitInfo!.Digits is null) return country;

      digitInfo = digitInfo.Digits[digitIndex];
    }
    if (digitInfo is null) return country;

    country = digitInfo.Country??country;
  }
  return country;
}