URI字符串解析

2024-01-23 18:08 更新

本模塊提供URI字符串解析的相關(guān)功能。

說明

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

導(dǎo)入模塊

  1. import uri from '@ohos.uri'

URI

構(gòu)造URI對(duì)象,提供判斷對(duì)象相等、規(guī)范路徑等方法。

屬性

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

名稱

類型

可讀

可寫

說明

scheme

string

獲取URI 的協(xié)議部分。

userInfo

string

獲取 URI 的用戶信息部分。

host

string

獲取 URI 的主機(jī)名部分(不帶端口)。

port

string

獲取 URI 的端口部分。

path

string

獲取 URI 的路徑部分。

query

string

獲取 URI 的查詢部分。

fragment

string

獲取 URI 的片段部分

authority

string

獲取此URI的解碼權(quán)限組件部分。

ssp

string

獲取URI的解碼方案特定部分。

命名規(guī)則

命名形式:

標(biāo)準(zhǔn)uri定義由以下三個(gè)部分組成

[scheme:]scheme-specific-part[#fragment]

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

URI示例:

  1. const result1 = new uri.URI("ftp://ftp.aaa.bbb.ccc/dddd/eee.txt");
  2. console.log(result1.host) // ftp.aaa.bbb.ccc
  3. console.log(result1.fragment) // null
  4. console.log(result1.path) // /dddd/eee.txt
  5. console.log(result1.scheme) // ftp
  6. console.log(result1.userInfo) // null
  7. console.log(result1.port) // -1
  8. console.log(result1.query) // null
  9. const result2 = new uri.URI("gopher://spinaltap.micro.umn.edu/00/Weather/California/Los%20Angeles#fragment");
  10. console.log(result2.host) // spinaltap.micro.umn.edu
  11. console.log(result2.fragment) // fragment
  12. console.log(result2.path) // /00/Weather/California/Los Angeles
  13. console.log(result2.scheme) // gopher
  14. console.log(result2.userInfo) // null
  15. console.log(result2.port) //-1
  16. console.log(result2.query) // null
  17. const result3 = new uri.URI("datashare:///com.samples.datasharetest.DataShare/DB00/TBL00");
  18. console.log(result3.host) // null
  19. console.log(result3.fragment) // null
  20. console.log(result3.path) // /com.samples.datasharetest.DataShare/DB00/TBL00
  21. console.log(result3.scheme) // datashare
  22. console.log(result3.userInfo) // null
  23. console.log(result3.port) // -1
  24. console.log(result3.query) // null
  25. const result4 = new uri.URI("https://username:password@host:8080/directory/file?foo=1&bar=2#fragment");
  26. console.log(result4.host) // host
  27. console.log(result4.fragment) // fragment
  28. console.log(result4.path) // /directory/file
  29. console.log(result4.scheme) // https
  30. console.log(result4.userInfo) // username:password
  31. console.log(result4.port) // 8080
  32. console.log(result4.query) // foo=1&bar=2
  33. const result5 = new uri.URI("dataability:///com.example.DataAbility");
  34. console.log(result5.host) // null
  35. console.log(result5.fragment) // null
  36. console.log(result5.path) // /com.example.DataAbility:
  37. console.log(result5.scheme) // dataability
  38. console.log(result5.userInfo) // null
  39. console.log(result5.port) // -1
  40. console.log(result5.query) // null

constructor

constructor(uri: string)

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

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

參數(shù):

參數(shù)名

類型

必填

說明

uri

string

入?yún)?duì)象。

錯(cuò)誤碼:

以下錯(cuò)誤碼的詳細(xì)介紹請(qǐng)參見語言基礎(chǔ)類庫錯(cuò)誤碼。

錯(cuò)誤碼ID

錯(cuò)誤信息

10200002

Invalid uri string.

示例:

  1. let mm = 'https://username:password@host:8080/directory/file?foo=1&bar=2#fragment';
  2. new uri.URI(mm); // Output 'https://username:password@host:8080/directory/file?foo=1&bar=2#fragment';
  1. 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)址的字符串序列化。

示例:

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

equals(deprecated)

equals(other: URI): boolean

判斷此URI是否與其他URI對(duì)象相等。

說明

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

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

參數(shù):

參數(shù)名

類型

必填

說明

other

URI

需要比較的URI對(duì)象。

返回值:

類型

說明

boolean

返回true表示相等,否則返回false。

示例:

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

equalsTo9+

equalsTo(other: URI): boolean

判斷此URI是否與其他URI對(duì)象相等。

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

參數(shù):

參數(shù)名

類型

必填

說明

other

URI

需要比較的URI對(duì)象。

返回值:

類型

說明

boolean

返回true表示相等,否則返回false。

示例:

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

checkIsAbsolute

checkIsAbsolute(): boolean

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

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

返回值:

類型

說明

boolean

如果是絕對(duì)URI返回true,否則返回false。

示例:

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

normalize

normalize(): URI

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

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

返回值:

類型

說明

URI

返回一個(gè)path被規(guī)范化后的URI對(duì)象。

示例:

  1. const uriInstance = new uri.URI('https://username:password@www.qwer.com:8080/path/path1/../path2/./path3?query=pppppp');
  2. console.log(uriInstance.path); // /path/path1/../path2/./path3
  3. let uriInstance1 = uriInstance.normalize();
  4. console.log(uriInstance1.path); // /path/path2/path3
以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)