HarmonyOS:电话号码格式化

ops/2025/2/13 17:49:13/

一、使用场景

不同国家和地区的电话号码在号码位数、组合方式、呈现方式等都存在差异。同时,在不同环境和条件下,电话号码可能存在不同的拨打方式和号码格式。例如,在中国境内跨地区打电话,通常需要先输入“0”,再拨打区号和八位电话号码,而在香港或澳门拨打电话时,需要不同的拨号方式。

通过号码格式化,可以根据需要给用户展示特定格式的电话号码。

二、开发步骤

电话号码格式化通过PhoneNumberFormat的format接口实现,具体开发步骤如下

2.1 导入模块。
import { i18n } from '@kit.LocalizationKit';
2.2 创建PhoneNumberFormat对象。

构造函数支通过PhoneNumberFormatOptions设置不同的电话号码格式,具体请参考表1。

let phoneNumberFormat: i18n.PhoneNumberFormat = new i18n.PhoneNumberFormat(country: string, options?: PhoneNumberFormatOptions);
2.3 电话号码格式化
let formattedPhoneNumber: string = phoneNumberFormat.format(phoneNumber: string);
2.4 判断电话号码正确性和号码归属地
let isValidNumber: boolean = phoneNumberFormat.isValidNumber(phoneNumber: string); // 判断电话号码正确性
let locationName: string = phoneNumberFormat.getLocationName(number: string, locale: string); // 获取号码归属地
2.5 电话号码格式化选项

以电话号码158****2312,所属国家代码CN为例,说明PhoneNumberFormatOptions不同取值和显示效果。

表1 电话号码格式化的类型(type)

取值显示效果
E164+86 158****2312
INTERNATIONAL+86 158 **** 2312
NATIONAL158 **** 2312
RFC3966tel:+86-158-****-2312
TYPING158 ***
2.6 开发实例

效果图

在这里插入图片描述

TestphoneNumberFormat.ets代码

// 导入模块
import { i18n } from '@kit.LocalizationKit';// 格式化电话号码
let phoneNumberFormat1 = new i18n.PhoneNumberFormat('CN');
let formattedPhoneNumber1 = phoneNumberFormat1.format('158****2312'); // formattedPhoneNumber1: 158 **** 2312
console.log(` 格式化电话号码 formattedPhoneNumber1:${formattedPhoneNumber1}`)// RFC3966类型的电话号码
let phoneNumberFormat2 = new i18n.PhoneNumberFormat('CN', { type: 'RFC3966' });
let formattedPhoneNumber2 = phoneNumberFormat2.format('158****2312'); // formattedPhoneNumber2: tel:+86-158-****-2312
console.log(` RFC3966类型的电话号码 formattedPhoneNumber2:${formattedPhoneNumber2}`)// 判断电话号码是否有效
let phoneNumberFormat3 = new i18n.PhoneNumberFormat('CN');
let isValid = phoneNumberFormat3.isValidNumber('158****2312'); // isValid: true
console.log(` 判断电话号码是否有效 isValid:${isValid}`)// 以某种语言显示号码归属地
let phoneNumberFormat4 = new i18n.PhoneNumberFormat("CN");
let locationName4 = phoneNumberFormat4.getLocationName('158****2312', 'en-GB') // locationName4: XiAn, Shanxi
console.log(` 以某种语言显示号码归属地 locationName4:${locationName4}`)// 拨号中的电话号码格式化
let phoneNumberFmt = new i18n.PhoneNumberFormat('CN', { type: 'TYPING' });
let phoneNumber: string = "0755453";
let formatResult: string = "";
for (let i = 0; i < phoneNumber.length; i++) {formatResult += phoneNumber.charAt(i);formatResult = phoneNumberFmt.format(formatResult);
}
console.log(` 拨号中的电话号码格式化 formatResult:${formatResult}`); // formatResult: 0755 453@Entry
@Component
struct TestphoneNumberFormat {@State message: string = '电话号码格式化';build() {Column({ space: 16 }) {Text(this.message).id('TestphoneNumberFormatHelloWorld').fontSize(20).fontWeight(FontWeight.Bold)Text(`格式化电话号码:${formattedPhoneNumber1}`).fontSize(20).fontWeight(FontWeight.Medium)Text(`RFC3966类型的电话号码:${formattedPhoneNumber2}`).fontSize(20).fontWeight(FontWeight.Medium)Text(`判断电话号码是否有效:${isValid}`).fontSize(20).fontWeight(FontWeight.Medium)Text(`以某种语言显示号码归属地:${locationName4}`).fontSize(20).fontWeight(FontWeight.Medium)Text(`拨号中的电话号码格式化:${formatResult}`).fontSize(20).fontWeight(FontWeight.Medium)}.height('100%').width('100%')}
}

http://www.ppmy.cn/ops/157072.html

相关文章

如何使用Ollama部署大模型

正文 Ollama简介 Ollama是一个专为在本地环境中运行和定制大型语言模型而设计的工具。它提供了一个简单而高效的接口&#xff0c;用于创建、运行和管理这些模型&#xff0c;同时还提供了一个丰富的预构建模型库&#xff0c;可以轻松集成到各种应用程序中。Ollama的目标是使大型…

基于yolov11的阿尔兹海默症严重程度检测系统python源码+onnx模型+评估指标曲线+精美GUI界面

【算法介绍】 基于YOLOv11的阿尔兹海默症严重程度检测系统是一种创新的医疗辅助工具&#xff0c;旨在通过先进的计算机视觉技术提高阿尔兹海默症的早期诊断和病情监测效率。阿尔兹海默症是一种渐进性的神经退行性疾病&#xff0c;通常表现为认知障碍、记忆丧失和语言障碍等症状…

R语言 文本分析 天龙八部

起因, 目的: 前面有人对 “倚天屠龙记” 进行分析,我这里只是进行模仿而已。 完整的文件, 已经绑定了,反正读者可以找一下。 案例背景 小说《天龙八部》是金庸先生所著的武侠小说,也是“射雕三部曲”的前传。全书共50章,字数超过一百万字。故事发生在北宋末年,以大理…

蓝桥杯嵌入式备赛(三)—— LED +按键 + LCD

目录 一、LED1、原理图介绍2、程序代码 二、按键1、原理图介绍2、程序代码 三、LCD1、原理图介绍2、程序代码 一、LED 1、原理图介绍 如果所示&#xff0c;STM32G431RBT6中有八个LED&#xff0c;由八个GPIO控制&#xff0c;分别为PC8-15&#xff0c;当输出为低电平时点亮。其中…

spring 中 AspectJ 基于注解的实现分析

前面的文章介绍了 spring 引入 AspectJ 后&#xff0c;基于注解的 AOP 实现&#xff0c;今天我们来分析下这个实现的具体逻辑是怎么样的。 aspectj-autoproxy 解析 我们知道&#xff0c;基于注解的实现&#xff0c;在 xml 中除了需要配置切面类&#xff0c;还需要配置 aspecj…

使用rustDesk搭建私有远程桌面

一、rustDesk官方网站 RustDesk – 开源远程桌面访问软件 二、下载地址 客户端&#xff1a;https://github.com/rustdesk/rustdesk/releases/tag/1.2.7 服务端&#xff1a;https://github.com/rustdesk/rustdesk-server/releases/tag/1.1.11-1 三、服务端安装&#xff08;…

Visual Basic语言的区块链

Visual Basic语言与区块链技术的结合 引言 在当今数字化迅猛发展的时代&#xff0c;区块链技术以其去中心化、安全性和透明性等特点&#xff0c;逐渐成为各行各业的重要解决方案。与此同时&#xff0c;作为一种古老而经典的编程语言&#xff0c;Visual Basic&#xff08;VB&a…

RISCV基础知识

一、ISA指令集 ISA 命名格式&#xff1a;RV [###] [abc…xyz] RV&#xff1a;用于标识 RISC-V体系架构的前缀&#xff0c;既 RISC-V 的缩写。 [###] &#xff1a;{32, 64, 128} 用于标识处理器的字宽&#xff0c;也就是处理器的寄存器的宽度&#xff08;单位为bit&#xff0…