1# @ohos.uri (URI String Parsing) 2 3The uri module provides APIs for parsing URI strings that comply with the RFC3986 standard. This standard defines how to encode and parse the identifiers used to locate network resources. The module does not support parsing of URIs in non-standard scenarios. 4 5> **NOTE** 6> 7> The initial APIs of this module are supported since API version 8. Newly added APIs will be marked with a superscript to indicate their earliest API version. 8 9 10## Modules to Import 11 12```ts 13import { uri } from '@kit.ArkTS'; 14``` 15 16## URI 17 18Implements a URI, which provides APIs for determining whether objects are equal as well as standard paths. 19 20### Attributes 21 22**System capability**: SystemCapability.Utils.Lang 23 24| Name| Type| Readable| Writable| Description| 25| -------- | -------- | -------- | -------- | -------- | 26| scheme | string | Yes| No| Scheme in the URI. If this part does not exist, a null object is returned.<br>**Atomic service API**: This API can be used in atomic services since API version 11.| 27| userInfo | string | Yes| No| User information in the URI. If this part does not exist, a null object is returned.<br>**Atomic service API**: This API can be used in atomic services since API version 11.| 28| host | string | Yes| No| Host name (without the port number) in the URI. If this part does not exist, a null object is returned.<br>**Atomic service API**: This API can be used in atomic services since API version 11.| 29| port | string | Yes| No| Port number in the URI.<br>**Atomic service API**: This API can be used in atomic services since API version 11.| 30| path | string | Yes| No| Path in the URI. If this part does not exist, a null object is returned.<br>**Atomic service API**: This API can be used in atomic services since API version 11.| 31| query | string | Yes| No| Query parameters in the URI. If this part does not exist, a null object is returned.<br>**Atomic service API**: This API can be used in atomic services since API version 11.| 32| fragment | string | Yes| No| Fragments in the URI. If this part does not exist, a null object is returned.<br>**Atomic service API**: This API can be used in atomic services since API version 11.| 33| authority | string | Yes| No| Authority in the URI. If this part does not exist, a null object is returned.<br>**Atomic service API**: This API can be used in atomic services since API version 11.| 34| ssp | string | Yes| No| Scheme-specific part in the URI. It contains protocol-or scheme-specific information.<br>**Atomic service API**: This API can be used in atomic services since API version 11.| 35| encodedUserInfo<sup>12+</sup> | string | Yes | No | Encoded user information in the URI. If this part does not exist, a null object is returned.<br>**Atomic service API**: This API can be used in atomic services since API version 12. | 36| encodedPath<sup>12+</sup> | string | Yes | No | Encoded path in the URI. If this part does not exist, a null object is returned.<br>**Atomic service API**: This API can be used in atomic services since API version 12. | 37| encodedQuery<sup>12+</sup> | string | Yes | No | Encoded query parameters in the URI. If this part does not exist, a null object is returned.<br>**Atomic service API**: This API can be used in atomic services since API version 12. | 38| encodedFragment<sup>12+</sup> | string | Yes | No | Encoded fragments in the URI. If this part does not exist, a null object is returned.<br>**Atomic service API**: This API can be used in atomic services since API version 12. | 39| encodedAuthority<sup>12+</sup> | string | Yes | No | Encoded authority in the URI. If this part does not exist, a null object is returned.<br>**Atomic service API**: This API can be used in atomic services since API version 12. | 40| encodedSSP<sup>12+</sup> | string | Yes | No | Encoded scheme-specific part in the URI.<br>**Atomic service API**: This API can be used in atomic services since API version 12. | 41 42### Naming Rules 43 44Naming format: 45 46A standard URI mainly consists of three parts, as follows: 47 48[scheme:]scheme-specific-part[#fragment] 49 50The generic URI syntax consists of a hierarchical sequence of components, as follows: 51 52[scheme:][//authority][path][?query][#fragment] 53 54It can be further divided into the following parts: 55 56[scheme:][//[user-info@]host[:port]][path][?query][#fragment] 57 58- scheme: scheme name, which is separated from scheme-specific-part by a colon (:). The URI that contains the scheme component is an absolute URI, and the URI that does not contain the scheme component is a relative URI. Set this part as required. Example values: **http**, **https**, **ftp**, and **datashare**. 59- scheme-specific-part: specific part of the URI decoding scheme. It is located between [scheme:] and [#fragment] and consists of [//][authority][path][?query]. The URI that starts with a slash (/) is a hierarchical URI, and the URI that does not start with a slash (/) is an opaque URI. Set this part as required. 60 - authority: decoding authority component of the URI. The value consists of [userinfo@]host[:port]. Set this part as required. 61 - userinfo: user information, which is separated from host by an at sign (@). Set this part as required. 62 - host: host name of the server. This parameter is mandatory when authority exists. 63 - port: port number of the server. The default value is **-1**. Set this part as required. 64 - path: path information, which is located between host and query and separated by a slash (/). Set this part as required. 65 - query: query component, which is located between path and fragment, indicated by the first question mark (?) character, and is in the format of key-value pairs. Multiple key-value pairs are separated by the at sign (&), and the key and value in a pair is separated by the equal sign (=). Set this part as required. 66- fragment: fragment component, which is separated from scheme-specific-part by the pound key (#). Set this part as required. 67 68**Example URIs** 69 70```ts 71const result1 = new uri.URI("ftp://ftp.aaa.bbb.ccc/dddd/eee.txt"); 72console.info(result1.host) // ftp.aaa.bbb.ccc 73console.info(result1.fragment) // null 74console.info(result1.path) // /dddd/eee.txt 75console.info(result1.scheme) // ftp 76console.info(result1.userInfo) // null 77console.info(result1.port) // -1 78console.info(result1.query) // null 79 80const result2 = new uri.URI("gopher://spinaltap.micro.umn.edu/00/Weather/California/Los%20Angeles#fragment"); 81console.info(result2.host) // spinaltap.micro.umn.edu 82console.info(result2.fragment) // fragment 83console.info(result2.path) // /00/Weather/California/Los Angeles 84console.info(result2.scheme) // gopher 85console.info(result2.userInfo) // null 86console.info(result2.port) //-1 87console.info(result2.query) // null 88 89const result3 = new uri.URI("datashare:///com.samples.datasharetest.DataShare/DB00/TBL00"); 90console.info(result3.host) // null 91console.info(result3.fragment) // null 92console.info(result3.path) // /com.samples.datasharetest.DataShare/DB00/TBL00 93console.info(result3.scheme) // datashare 94console.info(result3.userInfo) // null 95console.info(result3.port) // -1 96console.info(result3.query) // null 97 98const result4 = new uri.URI("https://username:password@host:8080/directory/file?foo=1&bar=2#fragment"); 99console.info(result4.host) // host 100console.info(result4.fragment) // fragment 101console.info(result4.path) // /directory/file 102console.info(result4.scheme) // https 103console.info(result4.userInfo) // username:password 104console.info(result4.port) // 8080 105console.info(result4.query) // foo=1&bar=2 106 107const result5 = new uri.URI("dataability:///com.example.DataAbility"); 108console.info(result5.host) // null 109console.info(result5.fragment) // null 110console.info(result5.path) // /com.example.DataAbility: 111console.info(result5.scheme) // dataability 112console.info(result5.userInfo) // null 113console.info(result5.port) // -1 114console.info(result5.query) // null 115 116const result6 = new uri.URI("https://username:my+name@host:8080/directory/my+file?foo=1&bar=2#fragment"); 117console.info(result6.encodedUserInfo) // username:my+name 118console.info(result6.encodedPath) // /directory/my+file 119console.info(result6.encodedQuery) // foo=1&bar=2 120console.info(result6.encodedFragment) // fragment 121console.info(result6.encodedAuthority) // username:my+name@host:8080 122console.info(result6.encodedSSP) // //username:my+name@host:8080/directory/my+file?foo=1&bar=2 123 124let result7 = new uri.URI("www.abc.com:8080/directory/file?ab=pppppp#qwer=da"); 125console.log(result7.scheme) // www.abc.com 126console.log(result7.host) // null 127console.log(result7.port) // -1 128console.log(result7.path) // null 129console.log(result7.query) // null 130console.log(result7.authority) // null 131console.log(result7.fragment) // qwer=da 132console.log(result7.ssp) // 8080/directory/file?ab=pppppp 133console.log(result7.checkIsAbsolute()) // true 134``` 135 136### constructor 137 138constructor(uri: string) 139 140A constructor used to create a URI instance. 141 142**Atomic service API**: This API can be used in atomic services since API version 11. 143 144**System capability**: SystemCapability.Utils.Lang 145 146**Parameters** 147 148| Name| Type| Mandatory| Description| 149| -------- | -------- | -------- | -------- | 150| uri | string | Yes| Input object.| 151 152**Error codes** 153 154For details about the error codes, see [Universal Error Codes](../errorcode-universal.md) and [Utils Error Codes](errorcode-utils.md). 155 156| ID| Error Message| 157| -------- | -------- | 158| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. | 159| 10200002 | Invalid uri string. | 160 161**Example** 162 163```ts 164let mm = 'https://username:password@host:8080/directory/file?foo=1&bar=2#fragment'; 165new uri.URI(mm); 166``` 167```ts 168new uri.URI('https://username:password@host:8080'); 169``` 170 171 172### toString 173 174toString(): string 175 176Converts this URI into an encoded string. 177 178**System capability**: SystemCapability.Utils.Lang 179 180**Atomic service API**: This API can be used in atomic services since API version 11. 181 182**Return value** 183 184| Type| Description| 185| -------- | -------- | 186| string | URI in a serialized string.| 187 188**Example** 189 190```ts 191const result = new uri.URI('https://username:password@host:8080/directory/file?ab=pppppp#qwer da'); 192let result1 = result.toString(); // https://username:password@host:8080/directory/file?ab=pppppp#qwer%20da 193``` 194 195### equalsTo<sup>9+</sup> 196 197equalsTo(other: URI): boolean 198 199Checks whether this URI is the same as another URI object. 200 201**Atomic service API**: This API can be used in atomic services since API version 11. 202 203**System capability**: SystemCapability.Utils.Lang 204 205**Parameters** 206 207| Name| Type| Mandatory| Description| 208| -------- | -------- | -------- | -------- | 209| other | [URI](#uri) | Yes| URI object to compare.| 210 211**Return value** 212 213| Type| Description| 214| -------- | -------- | 215| boolean | Returns **true** if the two URIs are the same; returns **false** otherwise.| 216 217**Error codes** 218 219For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 220 221| ID| Error Message| 222| -------- | -------- | 223| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 224 225**Example** 226 227```ts 228const uriInstance = new uri.URI('https://username:password@host:8080/directory/file?query=pppppp#qwer=da'); 229const uriInstance1 = new uri.URI('https://username:password@host:8080/directory/file?query=pppppp#qwer=da'); 230let result = uriInstance.equalsTo(uriInstance1); // true 231``` 232 233### checkIsAbsolute 234 235checkIsAbsolute(): boolean 236 237Checks whether this URI is an absolute URI (whether the scheme component is defined). 238 239**Atomic service API**: This API can be used in atomic services since API version 11. 240 241**System capability**: SystemCapability.Utils.Lang 242 243**Return value** 244 245| Type| Description| 246| -------- | -------- | 247| boolean | **true**: The URI is an absolute URI.<br>**false**: The URI is not an absolute URI.| 248 249**Example** 250 251```ts 252const uriInstance = new uri.URI('https://username:password@www.qwer.com:8080?query=pppppp'); 253console.info(`${uriInstance.checkIsAbsolute()}`); // true 254const uriInstance1 = new uri.URI('xxx.com/suppliers.htm'); 255console.info(`${uriInstance1.checkIsAbsolute()}`); // false 256``` 257 258 259### normalize 260 261normalize(): URI 262 263Normalizes the path of this URI. 264 265**Atomic service API**: This API can be used in atomic services since API version 11. 266 267**System capability**: SystemCapability.Utils.Lang 268 269**Return value** 270 271| Type| Description| 272| -------- | -------- | 273| [URI](#uri) | URI with the normalized path.| 274 275**Example** 276 277```ts 278const uriInstance = new uri.URI('https://username:password@www.qwer.com:8080/path/path1/../path2/./path3?query=pppppp'); 279console.info(uriInstance.path); // /path/path1/../path2/./path3 280let uriInstance1 = uriInstance.normalize(); 281console.info(uriInstance1.path); // /path/path2/path3 282``` 283 284### checkRelative<sup>12+</sup> 285 286checkRelative(): boolean 287 288Checks whether this URI is a relative URI. A relative URI does not contain the scheme component. 289 290**Atomic service API**: This API can be used in atomic services since API version 12. 291 292**System capability**: SystemCapability.Utils.Lang 293 294**Return value** 295 296| Type | Description | 297| ------- | ------------------------------------------ | 298| boolean | **true**: The URI is a relative URI.<br>**false**: The URI is not a relative URI.| 299 300**Example** 301 302```ts 303const uriInstance = new uri.URI("https://username:password@www.qwer.com:8080?query=p"); 304console.info(`${uriInstance.checkRelative()}`); // false 305const uriInstance1 = new uri.URI("/images/pic.jpg"); 306console.info(`${uriInstance1.checkRelative()}`); // true 307``` 308 309### checkOpaque<sup>12+</sup> 310 311checkOpaque(): boolean 312 313Checks whether this URI is an opaque URI. The URI that does not start with a slash (/) is an opaque URI. 314 315**Atomic service API**: This API can be used in atomic services since API version 12. 316 317**System capability**: SystemCapability.Utils.Lang 318 319**Return value** 320 321| Type | Description | 322| ------- | ---------------------------------------------- | 323| boolean | **true**: The URI is an opaque URI.<br>**false**: The URI is not an opaque URI.| 324 325**Example** 326 327```ts 328const uriInstance = new uri.URI("http://www.test.com/images/pic.jpg"); 329console.info(`${uriInstance.checkOpaque()}`); // false 330const uriInstance1 = new uri.URI("mailto:user@example.com"); 331console.info(`${uriInstance1.checkOpaque()}`); // true 332``` 333 334### checkHierarchical<sup>12+</sup> 335 336checkHierarchical(): boolean 337 338Checks whether this URI is a hierarchical URI. The URI that starts with a slash (/) in scheme-specific-part is a hierarchical URI. Relative URIs are also hierarchical. 339 340**Atomic service API**: This API can be used in atomic services since API version 12. 341 342**System capability**: SystemCapability.Utils.Lang 343 344**Return value** 345 346| Type | Description | 347| ------- | -------------------------------------------- | 348| boolean | **true**: The URI is a hierarchical URI.<br>**false**: The URI is not a hierarchical URI.| 349 350**Example** 351 352```ts 353const uriInstance = new uri.URI("http://www.test.com/images/pic.jpg"); 354console.info(`${uriInstance.checkHierarchical()}`); // true 355const uriInstance1 = new uri.URI("mailto:user@example.com"); 356console.info(`${uriInstance1.checkHierarchical()}`); // false 357``` 358 359### getQueryValue<sup>12+</sup> 360 361getQueryValue(key:string): string 362 363Obtains the first value of a given key from the query component of this URI. If the query component contains encoded content, this API decodes the key before obtaining the value. 364 365The query component follows the question mark (?) and consists of key-value pairs, separated by the at sign (&). In each key-value pair, the equal sign (=) is used to connect the key and value. 366 367**Atomic service API**: This API can be used in atomic services since API version 12. 368 369**System capability**: SystemCapability.Utils.Lang 370 371**Parameters** 372 373| Name| Type | Mandatory| Description | 374| ------ | ------ | ---- | ----------------------- | 375| key | string | Yes | Key of the URI query parameter.| 376 377**Return value** 378 379| Type | Description | 380| ------ | ----------------------------- | 381| string | First value obtained. If no value is found, a null object is returned.| 382 383**Error codes** 384 385For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 386 387| ID| Error Message| 388| -------- | -------- | 389| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 390 391**Example** 392 393```ts 394const uriInstance = new uri.URI("https://www.com?param1=value1¶m2=value2"); 395console.info(uriInstance.getQueryValue("param1")); // value1 396let uriInstance1 = new uri.URI('https://www.zyy.ss?sa%3D=po%7E'); 397console.info(uriInstance1.getQueryValue('sa=')) // po~ 398console.info(uriInstance1.getQueryValue('abc')) // null 399``` 400 401### addQueryValue<sup>12+</sup> 402 403addQueryValue(key:string, value:string): URI 404 405Adds a query parameter to this URI to create a new URI, while keeping the existing URI unchanged. 406 407**Atomic service API**: This API can be used in atomic services since API version 12. 408 409**System capability**: SystemCapability.Utils.Lang 410 411**Parameters** 412 413| Name| Type | Mandatory| Description | 414| ------ | ------ | ---- | ------------------------ | 415| key | string | Yes | Key of the query parameter.| 416| value | string | Yes | Value of the query parameter. | 417 418**Return value** 419 420| Type| Description | 421| ---- | -------------------------------- | 422| [URI](#uri) | URI object with the query parameter.| 423 424**Error codes** 425 426For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 427 428| ID| Error Message| 429| -------- | -------- | 430| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 431 432**Example** 433 434```ts 435const uriInstance = new uri.URI("https://www.test.com"); 436const newRoute = uriInstance.addQueryValue("param1", "hello world"); 437console.info(newRoute.toString()); // https://www.test.com?param1=hello%20world 438``` 439 440### addSegment<sup>12+</sup> 441 442addSegment(pathSegment:string): URI 443 444Encodes a given field, appends it to the path component of this URI to create a new URI, and returns the new URI, while keeping the existing URI unchanged. 445 446**Atomic service API**: This API can be used in atomic services since API version 12. 447 448**System capability**: SystemCapability.Utils.Lang 449 450**Parameters** 451 452| Name | Type | Mandatory| Description | 453| ----------- | ------ | ---- | ------------------ | 454| pathSegment | string | Yes | Field to be appended to the path component.| 455 456**Return value** 457 458| Type| Description | 459| ---- | -------------------------------- | 460| [URI](#uri) | URI object with the appended field.| 461 462**Error codes** 463 464For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 465 466| ID| Error Message| 467| -------- | -------- | 468| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 469 470**Example** 471 472```ts 473const uriInstance = new uri.URI("http://www.test.com"); 474const newRoute = uriInstance.addSegment("my image.jpg"); 475console.info(newRoute.toString()); // http://www.test.com/my%20image.jpg 476``` 477 478### addEncodedSegment<sup>12+</sup> 479 480addEncodedSegment(pathSegment:string): URI 481 482Appends an encoded field to the path component of this URI to create a new URI and returns the new URI, while keeping the existing URI unchanged. 483 484**Atomic service API**: This API can be used in atomic services since API version 12. 485 486**System capability**: SystemCapability.Utils.Lang 487 488**Parameters** 489 490| Name | Type | Mandatory| Description | 491| ----------- | ------ | ---- | ------------------ | 492| pathSegment | string | Yes | Encoded field to be appended to the path component.| 493 494**Return value** 495 496| Type| Description | 497| ---- | -------------------------------- | 498| [URI](#uri) | URI object with the appended field.| 499 500**Error codes** 501 502For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 503 504| ID| Error Message| 505| -------- | -------- | 506| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 507 508**Example** 509 510```ts 511const uriInstance = new uri.URI("http://www.test.com"); 512const newRoute = uriInstance.addEncodedSegment("my%20image.jpg"); 513console.info(newRoute.toString()); // http://www.test.com/my%20image.jpg 514``` 515 516### getQueryNames<sup>12+</sup> 517 518getQueryNames(): string[] 519 520Obtains all non-repeated keys in the query component of this URI. The query component follows the question mark (?) and consists of key-value pairs, separated by the at sign (&). In each key-value pair, the equal sign (=) is used to connect the key and value. 521 522**Atomic service API**: This API can be used in atomic services since API version 12. 523 524**System capability**: SystemCapability.Utils.Lang 525 526**Return value** 527 528| Type | Description | 529| ----------- | ----------------------------------- | 530| string[] | Non-repeated keys in the query component.| 531 532**Example** 533 534```ts 535const uriInstance = new uri.URI("https://www.test.com?param1=value1¶m2=value2"); 536const paramNames = uriInstance.getQueryNames(); 537console.info(Array.from(paramNames).toString()); // param1,param2 538``` 539 540### getQueryValues<sup>12+</sup> 541 542getQueryValues(key:string): string[] 543 544Obtains the values of a given key from the query component of this URI. If the query component contains encoded content, this API decodes the keys before obtaining the values. 545 546The query component follows the question mark (?) and consists of key-value pairs, separated by the at sign (&). In each key-value pair, the equal sign (=) is used to connect the key and value. 547 548**Atomic service API**: This API can be used in atomic services since API version 12. 549 550**System capability**: SystemCapability.Utils.Lang 551 552**Parameters** 553 554| Name| Type | Mandatory| Description | 555| ------ | ------ | ---- | ----------------------- | 556| key | string | Yes | Key of the URI query parameter.| 557 558**Return value** 559 560| Type | Description | 561| -------- | ----------------------------------- | 562| string[] | Array of values obtained. If no value is found, an empty string array [] is returned.| 563 564**Error codes** 565 566For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 567 568| ID| Error Message| 569| -------- | -------- | 570| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 571 572**Example** 573 574```ts 575const uriInstance = new uri.URI("https://www.test.com/search?query=name&query=my"); 576console.info(uriInstance.getQueryValues("query").toString()); // name,my 577console.info(JSON.stringify(uriInstance.getQueryValues("abc"))); // [] 578``` 579 580### getBooleanQueryValue<sup>12+</sup> 581 582getBooleanQueryValue(key:string,defaultValue:boolean): boolean 583 584Obtains the value of the Boolean type of a query parameter in this URI. 585 586**Atomic service API**: This API can be used in atomic services since API version 12. 587 588**System capability**: SystemCapability.Utils.Lang 589 590**Parameters** 591 592| Name | Type | Mandatory| Description | 593| ------------ | ------- | ---- | ------------------------------------- | 594| key | string | Yes | Name of the query parameter. | 595| defaultValue | boolean | Yes | Default value.| 596 597**Return value** 598 599| Type | Description | 600| ------- | ---------------------------------------------------------------------- | 601| boolean | If the specified query parameter does not exist, the default value is returned. If the first value of the query parameter is **false** or **0**, **false** is returned. Otherwise, **true** is returned.| 602 603**Error codes** 604 605For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 606 607| ID| Error Message| 608| -------- | -------- | 609| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 610 611**Example** 612 613```ts 614const uriInstance = new uri.URI("https://www.test.com/search?active=true"); 615console.info(`${uriInstance.getBooleanQueryValue("active", false)}`); // true 616const uriInstance1 = new uri.URI("https://www.test.com/search"); 617console.info(`${uriInstance1.getBooleanQueryValue("active", false)}`); // false 618const uriInstance2 = new uri.URI("https://www.test.com/search?active=aa&active=false"); 619console.info(`${uriInstance2.getBooleanQueryValue("active", false)}`); // true 620const uriInstance3 = new uri.URI("https://www.test.com/search?active=0"); 621console.info(`${uriInstance3.getBooleanQueryValue("active", true)}`); // false 622``` 623 624### clearQuery<sup>12+</sup> 625 626clearQuery(): URI 627 628Clears the query component of this URI to create a new URI, while keeping the existing URI object unchanged. 629 630**Atomic service API**: This API can be used in atomic services since API version 12. 631 632**System capability**: SystemCapability.Utils.Lang 633 634**Return value** 635 636| Type| Description | 637| ---- | ------------------------------------- | 638| [URI](#uri) | URI object whose query component has been cleared.| 639 640**Example** 641 642```ts 643const uriInstance = new uri.URI("https://www.test.com?param1=value1"); 644console.info(uriInstance.clearQuery().toString()); // https://www.test.com 645``` 646 647### getLastSegment<sup>12+</sup> 648 649getLastSegment(): string 650 651Obtains the last segment of this URI. A path includes multiple segments, separated by slashes (/). The part that ends with a slash is not a segment. 652 653**Atomic service API**: This API can be used in atomic services since API version 12. 654 655**System capability**: SystemCapability.Utils.Lang 656 657**Return value** 658 659| Type| Description | 660| ---- | ----------------------------- | 661| string | Last segment of the URI.| 662 663**Example** 664 665```ts 666const uriInstance = new uri.URI("content://com.test.provider/files/image.jpg"); 667console.info(uriInstance.getLastSegment()); // image.jpg 668``` 669 670### getSegment<sup>12+</sup> 671 672getSegment(): string[] 673 674Obtains all segments of this URI. 675 676**Atomic service API**: This API can be used in atomic services since API version 12. 677 678**System capability**: SystemCapability.Utils.Lang 679 680**Return value** 681 682| Type | Description | 683| -------- | --------------------------- | 684| string[] | All segments of this URI.| 685 686**Example** 687 688```ts 689const uriInstance = new uri.URI("http://www.test.com/path/to/image.jpg"); 690console.info(uriInstance.getSegment().toString()); // path,to,image.jpg 691``` 692 693### createFromParts<sup>12+</sup> 694 695createFromParts(scheme: string, ssp: string, fragment: string): URI 696 697Creates a URI based on the provided scheme, scheme-specific-part, and fragment components. 698 699**Atomic service API**: This API can be used in atomic services since API version 12. 700 701**System capability**: SystemCapability.Utils.Lang 702 703**Parameters** 704 705| Name | Type | Mandatory| Description | 706| -------- | ------ | ---- | ------------------------------- | 707| scheme | string | Yes | Scheme of the URI. | 708| ssp | string | Yes | Scheme-specific-part of the URI.| 709| fragment | string | Yes | Fragment of this URI. The fragment component is the part following the number sign (#). | 710 711**Return value** 712 713| Type| Description | 714| ---- | ------------------------------------------------- | 715| [URI](#uri) | URI object obtained.| 716 717**Error codes** 718 719For details about the error codes, see [Universal Error Codes](../errorcode-universal.md). 720 721| ID| Error Message| 722| -------- | -------- | 723| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. | 724 725**Example** 726 727```ts 728const uriInstance = uri.URI.createFromParts("mailto", "no body", "top"); 729console.info(uriInstance.toString()); // mailto:no%20body#top 730``` 731 732### equals<sup>(deprecated)</sup> 733 734equals(other: URI): boolean 735 736Checks whether this URI is the same as another URI object. 737 738> **NOTE** 739> 740> This API is supported since API version 8 and deprecated since API version 9. You are advised to use [equalsTo<sup>9+</sup>](#equalsto9) instead. 741 742**System capability**: SystemCapability.Utils.Lang 743 744**Parameters** 745 746| Name| Type| Mandatory| Description| 747| -------- | -------- | -------- | -------- | 748| other | [URI](#uri) | Yes| URI object to compare.| 749 750**Return value** 751 752| Type| Description| 753| -------- | -------- | 754| boolean | Returns **true** if the two URIs are the same; returns **false** otherwise.| 755 756**Example** 757 758```ts 759const uriInstance = new uri.URI('https://username:password@host:8080/directory/file?query=pppppp#qwer=da'); 760const uriInstance1 = new uri.URI('https://username:password@host:8080/directory/file?query=pppppp#qwer=da'); 761uriInstance.equals(uriInstance1); // true 762``` 763