0
  • 聊天消息
  • 系統(tǒng)消息
  • 評論與回復
登錄后你可以
  • 下載海量資料
  • 學習在線課程
  • 觀看技術視頻
  • 寫文章/發(fā)帖/加入社區(qū)
會員中心
創(chuàng)作中心

完善資料讓更多小伙伴認識你,還能領取20積分哦,立即完善>

3天內不再提示

OpenHarmony語言基礎類庫【@ohos.uri (URI字符串解析)】

jf_46214456 ? 來源:jf_46214456 ? 作者:jf_46214456 ? 2024-04-24 21:40 ? 次閱讀

本模塊提供URI字符串解析的相關功能。

說明:

本模塊首批接口從API version 8開始支持。后續(xù)版本的新增接口,采用上角標單獨標記接口的起始版本。

導入模塊

import uri from '@ohos.uri'

URI

屬性

系統(tǒng)能力: SystemCapability.Utils.Lang

名稱類型可讀可寫說明
schemestring獲取URI 的協(xié)議部分。
userInfostring獲取 URI 的用戶信息部分。
hoststring獲取 URI 的主機名部分(不帶端口)。
portstring獲取 URI 的端口部分。
pathstring獲取 URI 的路徑部分。
querystring獲取 URI 的查詢部分。
fragmentstring獲取 URI 的片段部分
authoritystring獲取此URI的解碼權限組件部分。
sspstring獲取URI的解碼方案特定部分。

鴻蒙開發(fā)指導文檔:[gitee.com/li-shizhen-skin/harmony-os/blob/master/README.md]

搜狗高速瀏覽器截圖20240326151450.png

命名規(guī)則

命名形式:

標準uri定義由以下三個部分組成 [scheme:]scheme-specific-part[#fragment]

  • scheme: 協(xié)議名,根據(jù)需要填寫。例如http、https、ftp、datashare、dataability等。
  • scheme-specific-part: URI的特定解碼方案特定部分,由[//][authority][path][?query]組成,根據(jù)需要填寫。
    • authority: URI的解碼權限組件部分。由[userinfo@]host[:port]組成,根據(jù)需要填寫。
      • userinfo: 用戶信息,根據(jù)需要填寫。
      • host: 服務器的主機名部分,當authority存在時,此項必填。
      • port: 服務器端口,根據(jù)需要填寫。
    • path: 路徑信息,根據(jù)需要填寫。
    • query: 查詢部分,根據(jù)需要填寫。
  • fragment: 片段部分,根據(jù)需要填寫。

URI示例:

const result1 = new uri.URI("ftp://ftp.aaa.bbb.ccc/dddd/eee.txt");
console.log(result1.host) // ftp.aaa.bbb.ccc
console.log(result1.fragment) // null
console.log(result1.path) // /dddd/eee.txt
console.log(result1.scheme) // ftp
console.log(result1.userInfo) // null
console.log(result1.port) // -1
console.log(result1.query) // null

const result2 = new uri.URI("gopher://spinaltap.micro.umn.edu/00/Weather/California/Los%20Angeles#fragment");
console.log(result2.host) // spinaltap.micro.umn.edu
console.log(result2.fragment) // fragment
console.log(result2.path) // /00/Weather/California/Los Angeles
console.log(result2.scheme) // gopher
console.log(result2.userInfo) // null
console.log(result2.port) //-1
console.log(result2.query) // null

const result3 = new uri.URI("datashare:///com.samples.datasharetest.DataShare/DB00/TBL00");
console.log(result3.host) // null
console.log(result3.fragment) // null
console.log(result3.path) // /com.samples.datasharetest.DataShare/DB00/TBL00
console.log(result3.scheme) // datashare
console.log(result3.userInfo) // null
console.log(result3.port) // -1
console.log(result3.query) // null

const result4 = new uri.URI("https://username:password@host:8080/directory/file?foo=1&bar=2#fragment");
console.log(result4.host) // host
console.log(result4.fragment) // fragment
console.log(result4.path) // /directory/file
console.log(result4.scheme) // https
console.log(result4.userInfo) // username:password
console.log(result4.port) // 8080
console.log(result4.query) // foo=1&bar=2

const result5 = new uri.URI("dataability:///com.example.DataAbility");
console.log(result5.host) // null
console.log(result5.fragment) // null
console.log(result5.path) // /com.example.DataAbility:
console.log(result5.scheme) // dataability
console.log(result5.userInfo) // null
console.log(result5.port) // -1
console.log(result5.query) // null

constructor

constructor(uri: string)

constructor是URI的構造函數(shù)。

系統(tǒng)能力: SystemCapability.Utils.Lang

參數(shù)

參數(shù)名類型必填說明
uristring入?yún)ο蟆?/td>

錯誤碼:

以下錯誤碼的詳細介紹請參見[語言基礎類庫錯誤碼]。

錯誤碼ID錯誤信息
10200002Invalid uri string.

示例:

let mm = 'https://username:password@host:8080/directory/file?foo=1&bar=2#fragment';
new uri.URI(mm); // Output 'https://username:password@host:8080/directory/file?foo=1&bar=2#fragment';
new uri.URI('https://username:password@host:8080'); // Output 'https://username:password@host:8080';

toString

toString(): string

系統(tǒng)能力: SystemCapability.Utils.Lang

返回適用于URI中的查詢字符串。

返回值:

類型說明
string返回網(wǎng)址的字符串序列化。

示例:

const result = new uri.URI('https://username:password@host:8080/directory/file?query=pppppp#qwer=da');
let result1 = result.toString();

equals(deprecated)

equals(other: URI): boolean

判斷此URI是否與其他URI對象相等。

說明:

從API version 8開始支持,從API version 9開始廢棄,建議使用[equalsTo9+]替代。

系統(tǒng)能力: SystemCapability.Utils.Lang

參數(shù):

參數(shù)名類型必填說明
otherURI需要比較的URI對象。

返回值:

類型說明
boolean返回true表示相等,否則返回false。

示例:

const uriInstance = new uri.URI('https://username:password@host:8080/directory/file?query=pppppp#qwer=da');
const uriInstance1 = new uri.URI('https://username:password@host:8080/directory/file?query=pppppp#qwer=da');
uriInstance.equals(uriInstance1);

equalsTo9+

equalsTo(other: URI): boolean

判斷此URI是否與其他URI對象相等。

系統(tǒng)能力: SystemCapability.Utils.Lang

參數(shù):

參數(shù)名類型必填說明
otherURI需要比較的URI對象。

返回值:

類型說明
boolean返回true表示相等,否則返回false。

示例:

const uriInstance = new uri.URI('https://username:password@host:8080/directory/file?query=pppppp#qwer=da');
const uriInstance1 = new uri.URI('https://username:password@host:8080/directory/file?query=pppppp#qwer=da');
let result = uriInstance.equalsTo(uriInstance1);

checkIsAbsolute

checkIsAbsolute(): boolean

判斷此URI是否為絕對URI(是否定義了scheme組件)。

系統(tǒng)能力: SystemCapability.Utils.Lang

返回值:

類型說明
boolean如果是絕對URI返回true,否則返回false。

示例:

const uriInstance = new uri.URI('https://username:password@www.qwer.com:8080?query=pppppp');
console.log(`${uriInstance.checkIsAbsolute()}`); // true
const uriInstance1 = new uri.URI('xxx.com/suppliers.htm');
console.log(`${uriInstance1.checkIsAbsolute()}`); // false

normalize

HarmonyOSOpenHarmony鴻蒙文檔籽料:mau123789是v直接拿

normalize(): URI

規(guī)范化此URI的路徑。

系統(tǒng)能力: SystemCapability.Utils.Lang

返回值:

類型說明
URI返回一個path被規(guī)范化后的URI對象。

示例:

const uriInstance = new uri.URI('https://username:password@www.qwer.com:8080/path/path1/../path2/./path3?query=pppppp');
console.log(uriInstance.path); // /path/path1/../path2/./path3
let uriInstance1 = uriInstance.normalize();
console.log(uriInstance1.path); // /path/path2/path3

審核編輯 黃宇

聲明:本文內容及配圖由入駐作者撰寫或者入駐合作網(wǎng)站授權轉載。文章觀點僅代表作者本人,不代表電子發(fā)燒友網(wǎng)立場。文章及其配圖僅供工程師學習之用,如有內容侵權或者其他違規(guī)問題,請聯(lián)系本站處理。 舉報投訴
  • 字符串
    +關注

    關注

    1

    文章

    566

    瀏覽量

    20389
  • 鴻蒙
    +關注

    關注

    56

    文章

    2271

    瀏覽量

    42508
  • OpenHarmony
    +關注

    關注

    25

    文章

    3555

    瀏覽量

    15776
收藏 人收藏

    評論

    相關推薦

    C++字符串string

    string是C++編程語言中的字符串。在C++中字符串處理可以使用c語言字符串形式char *,也可以使用string
    的頭像 發(fā)表于 07-10 00:26 ?1085次閱讀
    C++<b class='flag-5'>字符串</b>string

    ArkTS語言基礎-解析

    被設計用來傳輸和存儲數(shù)據(jù),是一種可擴展標記語言語言基礎提供了[XML生成、解析與轉換]的能力。 URL、
    發(fā)表于 02-20 16:44

    鴻蒙原生應用開發(fā)-ArkTS語言基礎概述

    擴展標記語言。語言基礎提供了XML生成、解析與轉換的能力。 URL、URI構造和
    發(fā)表于 03-05 15:42

    實例解析Java字符串內存管理方法

    Java[1]語言字符串操作提供了豐富的支持,它將字符串封裝在三個中并提供多種字符串操作接口。在Java應用程序中,由于對
    發(fā)表于 10-27 10:20 ?1次下載
    實例<b class='flag-5'>解析</b>Java<b class='flag-5'>字符串</b>內存管理方法

    C語言字符串轉數(shù)字實現(xiàn)方法

    在C/C++語言中沒有專門的字符串變量,通常用字符數(shù)組來存放字符串字符串是以“\0”作為結束符。C/C++提供了豐富的
    發(fā)表于 11-14 17:50 ?1.3w次閱讀

    C++之字符串學習的總結

    一般我們在c語言要實現(xiàn)對字符串操作的話,一般是采用字符數(shù)組或者一組函數(shù)來實現(xiàn)的,為啥這樣做呢,那是因為c語言里面根本就沒有字符串類型的關鍵字
    的頭像 發(fā)表于 12-24 16:24 ?690次閱讀

    strtok拆分字符串

    就是字符格式。有些場景需要使用多個處理器協(xié)同工作,比如單片機+openmv,它們之間需要通信,可以采用字符格式的編碼方式。操作字符串,無非是兩件事兒:生成字符串
    發(fā)表于 01-13 15:46 ?8次下載
    strtok拆分<b class='flag-5'>字符串</b>

    C語言總結_字符串全方位練習

    C語言字符串全方位練習,涉及知識點:字符串解析、大小寫判斷、字符串插入、字符串刪除、
    的頭像 發(fā)表于 08-14 09:41 ?1366次閱讀

    C語言-字符串處理

    字符串在C語言里使用非常多,因為很多數(shù)據(jù)處理都是文本,也就是字符串,特別是設備交互、web網(wǎng)頁交互返回的幾乎都是文本數(shù)據(jù)。 這篇文章就介紹字符串定義、和基本處理的方法。
    的頭像 發(fā)表于 08-14 10:05 ?1655次閱讀

    關于STEP7功能字符串轉換

    libraries---Standard Libray---TI-S7 Converting Blocks) FC編號 功能名稱 描述 FC5 DI_STRING 雙整數(shù)轉字符串 FC16
    的頭像 發(fā)表于 10-10 10:50 ?3739次閱讀

    C語言字符串的引用方式

    在C語言程序中,字符串是存放在字符數(shù)組中的。 2. 用字符數(shù)組存放一個字符串,可以通過數(shù)組名和下標引用
    的頭像 發(fā)表于 03-10 14:57 ?1709次閱讀

    nuere-簡單小巧快速的字符串解析

    neure是一個簡單小巧的字符串解析, 我在開發(fā)aopt時為了優(yōu)化編譯時間而開發(fā)的替代regex的. 目前代碼架構非常簡單, 性能上比regex更快, 和nom的速度不相上下. 設
    的頭像 發(fā)表于 08-14 09:54 ?484次閱讀

    c語言字符串定義

    C語言是一種強大而廣泛使用的編程語言,字符串是其中一個非常重要的概念。在C語言中,字符串是由一系列字符
    的頭像 發(fā)表于 11-24 10:02 ?1453次閱讀

    C語言字符串編譯函數(shù)介紹

    在C語言中,字符串實際上是使用null字符O'終止的一維字符數(shù)組。因此,一個以null結尾的字符串,包含了組成
    的頭像 發(fā)表于 03-07 16:18 ?393次閱讀
    C<b class='flag-5'>語言</b><b class='flag-5'>字符串</b>編譯函數(shù)介紹

    HarmonyOS開發(fā)案例:【購物APP】

    提供URI字符串解析的相關功能。
    的頭像 發(fā)表于 04-24 20:47 ?331次閱讀
    HarmonyOS開發(fā)案例:【購物APP】