harmony-utils之FormatUtil,格式化工具類
harmony-utils 簡介與說明
harmony-utils 一款功能豐富且極易上手的HarmonyOS工具庫,借助眾多實用工具類,致力于助力開發者迅速構建鴻蒙應用。其封裝的工具涵蓋了APP、設備、屏幕、授權、通知、線程間通信、彈框、吐司、生物認證、用戶首選項、拍照、相冊、掃碼、文件、日志、異常捕獲、字符、字符串、數字、集合、日期、隨機、base64、加密、解密、JSON等一系列的功能和作,能夠滿足各種不同的開發需求。
picker_utils 是harmony-utils拆分出來的一個子庫,包含 PickerUtil、PhotoHelper、ScanUtil。
下載安裝ohpm i @pura/harmony-utils
ohpm i @pura/picker_utils
//全局初始化方法,在UIAbility的onCreate方法中初始化 AppUtil.init()
onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void {
AppUtil.init(this.context);
}
API方法與使用
isPhone 判斷傳入的電話號碼格式是否正確
let phone: string = "19869062586";
let bl = FormatUtil.isPhone(phone);
getPhoneFormat 對電話號碼進行格式化
let phone: string = "19869062586";
let format = FormatUtil.getPhoneFormat(phone);
getPhoneLocationName 獲取電話號碼歸屬地
let phone: string = "19869062586";
let locationName = FormatUtil.getPhoneLocationName(phone);
transliterator 將輸入字符串從源格式轉換為目標格式(中文漢字轉為拼音)
let str = FormatUtil.transliterator("中國");
getFormatPercentage 格式化百分比,將數字轉化從百分比字符串
let percentage = FormatUtil.getFormatPercentage(0.491);
LogUtil.error(`percentage: ${percentage}`);
getFormatPhone 格式化手機號碼,隱藏中間四位
let phone: string = "18969062528";
let str = FormatUtil.getFormatPhone(phone);
getFormatCardNo 格式化身份證號碼,隱藏中間部分數字
let str = FormatUtil.getFormatCardNo("110101199001011234");
LogUtil.error(`格式化后的身份證號: ${str}`);
getFormatFileSize 格式化文件大小
let str = FormatUtil.getFormatFileSize(1024102400);
LogUtil.error(`文件大小: ${str}`);
getTruncateText 縮短長文本,超出部分用省略號表示
let str = FormatUtil.getTruncateText("女人什么年齡是最好?任何年齡都可以。因為每個年齡段都有不一樣的美??碭X的穿搭你就會發現成熟女性的魅力,反而會更加的吸引人。", 20);
LogUtil.error(`縮短長文本: ${str}`);
getIconFont 解析iconFont字符
let str = FormatUtil.getIconFont("e631");
LogUtil.error(`getIconFont: ${str}`);
getQueryValue 獲取url里的參數,Key對應的Value
let url = "https://blog.csdn.net/article?spm=blog1024&name=李斯";
let value1 = FormatUtil.getQueryValue(url, 'spm');
let value2 = FormatUtil.getQueryValue(url, 'name');
let value3 = FormatUtil.getQueryValue(url, 'mona');
getParamsUrl 將參數拼接在url上,返回新的url
let map = new Map< string, Any >();
map.set("id", 100011);
map.set("name", '王五');
map.set("sex", undefined);
let url1 = FormatUtil.getParamsUrl("https://blog.csdn.net?id=100012", map);
let obj: Record< string, Object > = {};
obj['key'] = 'show';
obj['user'] = '王五';
obj['pd'] = 0;
obj['type'] = 'csaitab';
let url2 = FormatUtil.getParamsUrl("https://blog.csdn.net?id=100012", obj);
創作不易,請給童長老點贊
審核編輯 黃宇
聲明:本文內容及配圖由入駐作者撰寫或者入駐合作網站授權轉載。文章觀點僅代表作者本人,不代表電子發燒友網立場。文章及其配圖僅供工程師學習之用,如有內容侵權或者其他違規問題,請聯系本站處理。
舉報投訴
-
Harmony
+關注
關注
0文章
104瀏覽量
2984
發布評論請先 登錄
相關推薦
熱點推薦
harmony-utils之NumberUtil,Number工具類
harmony-utils之NumberUtil,Number工具類 harmony-utils 簡介與說明
harmony-utils之PreviewUtil,文件預覽工具類
harmony-utils之PreviewUtil,文件預覽工具類 harmony-utils 簡介與說明 [
harmony-utils之StrUtil,字符串工具類
harmony-utils之StrUtil,字符串工具類 harmony-utils 簡介與說明 [ha
harmony-utils之TypeUtil,類型檢查工具類
harmony-utils之TypeUtil,類型檢查工具類 harmony-utils 簡介與說明 [
harmony-utils之AuthUtil,生物認證相關工具類
# harmony-utils之AuthUtil,生物認證相關工具類 ## harmony-utils 簡介與說明 ------[
harmony-utils之ArrayUtil,集合工具類
# harmony-utils之ArrayUtil,集合工具類 ## harmony-utils 簡介與說明 ------[
評論