1# @ohos.url (URL String Parsing) 2 3The **url** module provides APIs for constructing [URLParams](#urlparams9) and [URL](#url) instances to process URL strings. 4 5> **NOTE** 6> 7> The initial APIs of this module are supported since API version 7. Newly added APIs will be marked with a superscript to indicate their earliest API version. 8 9## Modules to Import 10 11``` 12import Url from '@ohos.url' 13``` 14## URLParams<sup>9+</sup> 15 16Defines APIs for handling URL query strings. 17 18### constructor<sup>9+</sup> 19 20constructor(init?: string[][] | Record<string, string> | string | URLParams) 21 22A constructor used to create a **URLParams** instance. 23 24**System capability**: SystemCapability.Utils.Lang 25 26**Parameters** 27 28| Name| Type| Mandatory| Description| 29| -------- | -------- | -------- | -------- | 30| init | string[][] \| Record<string, string> \| string \| URLParams | No| Input parameter objects, which include the following:<br>- **string[][]**: two-dimensional string array<br>- **Record<string, string>**: list of objects<br>- **string**: string<br>- **URLParams**: object<br>The default value is **null**.| 31 32**Example** 33 34```ts 35let objectParams = new Url.URLParams([ ['user1', 'abc1'], ['query2', 'first2'], ['query3', 'second3'] ]); 36let objectParams1 = new Url.URLParams({"fod" : '1' , "bard" : '2'}); 37let objectParams2 = new Url.URLParams('?fod=1&bard=2'); 38let urlObject = Url.URL.parseURL('https://developer.mozilla.org/?fod=1&bard=2'); 39let params = new Url.URLParams(urlObject.search); 40``` 41 42 43### append<sup>9+</sup> 44 45append(name: string, value: string): void 46 47Appends a key-value pair into the query string. 48 49**System capability**: SystemCapability.Utils.Lang 50 51**Parameters** 52 53| Name| Type| Mandatory| Description| 54| -------- | -------- | -------- | -------- | 55| name | string | Yes| Key of the key-value pair to append.| 56| value | string | Yes| Value of the key-value pair to append.| 57 58**Example** 59 60```ts 61let urlObject = Url.URL.parseURL('https://developer.exampleUrl/?fod=1&bard=2'); 62let paramsObject = new Url.URLParams(urlObject.search.slice(1)); 63paramsObject.append('fod', '3'); 64``` 65 66 67### delete<sup>9+</sup> 68 69delete(name: string): void 70 71Deletes key-value pairs of the specified key. 72 73**System capability**: SystemCapability.Utils.Lang 74 75**Parameters** 76 77| Name| Type| Mandatory| Description| 78| -------- | -------- | -------- | -------- | 79| name | string | Yes| Key of the key-value pairs to delete.| 80 81**Example** 82 83```ts 84let urlObject = Url.URL.parseURL('https://developer.exampleUrl/?fod=1&bard=2'); 85let paramsObject = new Url.URLParams(urlObject.search.slice(1)); 86paramsObject.delete('fod'); 87``` 88 89 90### getAll<sup>9+</sup> 91 92getAll(name: string): string[] 93 94Obtains all the values based on the specified key. 95 96**System capability**: SystemCapability.Utils.Lang 97 98**Parameters** 99 100| Name| Type| Mandatory| Description| 101| -------- | -------- | -------- | -------- | 102| name | string | Yes| Target key.| 103 104**Return value** 105 106| Type| Description| 107| -------- | -------- | 108| string[] | All the values obtained.| 109 110**Example** 111 112```ts 113let urlObject = Url.URL.parseURL('https://developer.exampleUrl/?fod=1&bard=2'); 114let params = new Url.URLParams(urlObject.search.slice(1)); 115params.append('fod', '3'); // Add a second value for the fod parameter. 116console.log(params.getAll('fod').toString()) // Output ["1","3"]. 117``` 118 119 120### entries<sup>9+</sup> 121 122entries(): IterableIterator<[string, string]> 123 124Obtains an ES6 iterator. Each item of the iterator is a JavaScript array, and the first and second fields of each array are the key and value respectively. 125 126**System capability**: SystemCapability.Utils.Lang 127 128**Return value** 129 130| Type| Description| 131| -------- | -------- | 132| IterableIterator<[string, string]> | ES6 iterator.| 133 134**Example** 135 136```ts 137let searchParamsObject = new Url.URLParams("keyName1=valueName1&keyName2=valueName2"); 138let pair:Iterable<Object[]> = searchParamsObject.entries(); 139let arrayValue = Array.from(pair); 140for (let pair of arrayValue) { // Show keyName/valueName pairs 141 console.log(pair[0]+ ', '+ pair[1]); 142} 143``` 144 145 146### forEach<sup>9+</sup> 147 148forEach(callbackFn: (value: string, key: string, searchParams: URLParams) => void, thisArg?: Object): void 149 150Traverses the key-value pairs in the **URLSearchParams** instance by using a callback. 151 152**System capability**: SystemCapability.Utils.Lang 153 154**Parameters** 155 156| Name| Type| Mandatory| Description| 157| -------- | -------- | -------- | -------- | 158| callbackFn | function | Yes| Callback invoked to traverse the key-value pairs in the **URLSearchParams** instance.| 159| thisArg | Object | No| Value of **this** to use when **callbackFn** is invoked. The default value is this object.| 160 161**Table 1** callbackFn parameter description 162 163| Name| Type| Mandatory| Description| 164| -------- | -------- | -------- | -------- | 165| value | string | Yes| Value that is currently traversed.| 166| key | string | Yes| Key that is currently traversed.| 167| searchParams | [URLParams](#urlparams9) | Yes| Instance that invokes the **forEach** method.| 168 169**Example** 170 171```ts 172const myURLObject = Url.URL.parseURL('https://developer.exampleUrl/?fod=1&bard=2'); 173myURLObject.params.forEach((value, name, searchParams) => { 174 console.log(name, value, myURLObject.params === searchParams); 175}); 176``` 177 178 179### get<sup>9+</sup> 180 181get(name: string): string | null 182 183Obtains the value of the first key-value pair based on the specified key. 184 185**System capability**: SystemCapability.Utils.Lang 186 187**Parameters** 188 189| Name| Type| Mandatory| Description| 190| -------- | -------- | -------- | -------- | 191| name | string | Yes| Key specified to obtain the value.| 192 193**Return value** 194 195| Type| Description| 196| -------- | -------- | 197| string | Returns the value of the first key-value pair if obtained.| 198| null | Returns **null** if no value is obtained.| 199 200**Example** 201 202```ts 203let paramsObject = new Url.URLParams('name=Jonathan&age=18'); 204let name = paramsObject.get("name"); // is the string "Jonathan" 205let age = paramsObject.get("age"); // is the string "18" 206``` 207 208 209### has<sup>9+</sup> 210 211has(name: string): boolean 212 213Checks whether a key has a value. 214 215**System capability**: SystemCapability.Utils.Lang 216 217**Parameters** 218 219| Name| Type| Mandatory| Description| 220| -------- | -------- | -------- | -------- | 221| name | string | Yes| Key specified to search for its value.| 222 223**Return value** 224 225| Type| Description| 226| -------- | -------- | 227| boolean | Returns **true** if the value exists; returns **false** otherwise.| 228 229**Example** 230 231```ts 232let urlObject = Url.URL.parseURL('https://developer.exampleUrl/?fod=1&bard=2'); 233let paramsObject = new Url.URLParams(urlObject.search.slice(1)); 234let result = paramsObject.has('bard'); 235``` 236 237 238### set<sup>9+</sup> 239 240set(name: string, value: string): void 241 242Sets the value for a key. If key-value pairs matching the specified key exist, the value of the first key-value pair will be set to the specified value and other key-value pairs will be deleted. Otherwise, the key-value pair will be appended to the query string. 243 244**System capability**: SystemCapability.Utils.Lang 245 246**Parameters** 247 248| Name| Type| Mandatory| Description| 249| -------- | -------- | -------- | -------- | 250| name | string | Yes| Key of the value to set.| 251| value | string | Yes| Value to set.| 252 253**Example** 254 255```ts 256let urlObject = Url.URL.parseURL('https://developer.exampleUrl/?fod=1&bard=2'); 257let paramsObject = new Url.URLParams(urlObject.search.slice(1)); 258paramsObject.set('baz', '3'); // Add a third parameter. 259``` 260 261 262### sort<sup>9+</sup> 263 264sort(): void 265 266Sorts all key-value pairs contained in this object based on the Unicode code points of the keys and returns undefined. This method uses a stable sorting algorithm, that is, the relative order between key-value pairs with equal keys is retained. 267 268**System capability**: SystemCapability.Utils.Lang 269 270**Example** 271 272```ts 273let searchParamsObject = new Url.URLParams("c=3&a=9&b=4&d=2"); // Create a test URLSearchParams object 274searchParamsObject.sort(); // Sort the key/value pairs 275console.log(searchParamsObject.toString()); // Display the sorted query string // Output a=9&b=4&c=3&d=2 276``` 277 278 279### keys<sup>9+</sup> 280 281keys(): IterableIterator<string> 282 283Obtains an ES6 iterator that contains the keys of all the key-value pairs. 284 285**System capability**: SystemCapability.Utils.Lang 286 287**Return value** 288 289| Type| Description| 290| -------- | -------- | 291| IterableIterator<string> | ES6 iterator that contains the keys of all the key-value pairs.| 292 293**Example** 294 295```ts 296let searchParamsObject = new Url.URLParams("key1=value1&key2=value2"); // Create a URLSearchParamsObject object for testing 297let keys = Array.from(searchParamsObject.keys()); 298for (let key of keys) { // Output key-value pairs 299 console.log(key); 300} 301``` 302 303 304### values<sup>9+</sup> 305 306values(): IterableIterator<string> 307 308Obtains an ES6 iterator that contains the values of all the key-value pairs. 309 310**System capability**: SystemCapability.Utils.Lang 311 312**Return value** 313 314| Type| Description| 315| -------- | -------- | 316| IterableIterator<string> | ES6 iterator that contains the values of all the key-value pairs.| 317 318**Example** 319 320```ts 321let searchParams = new Url.URLParams("key1=value1&key2=value2"); // Create a URLSearchParamsObject object for testing 322let values = Array.from(searchParams.values()); 323for (let value of values) { 324 console.log(value); 325} 326``` 327 328 329### [Symbol.iterator]<sup>9+</sup> 330 331[Symbol.iterator]\(): IterableIterator<[string, string]> 332 333Obtains an ES6 iterator. Each item of the iterator is a JavaScript array, and the first and second fields of each array are the key and value respectively. 334 335**System capability**: SystemCapability.Utils.Lang 336 337**Return value** 338 339| Type| Description| 340| -------- | -------- | 341| IterableIterator<[string, string]> | ES6 iterator.| 342 343**Example** 344 345```ts 346const paramsObject = new Url.URLParams('fod=bay&edg=bap'); 347let iter: Iterable<Object[]> = paramsObject[Symbol.iterator](); 348let pairs = Array.from(iter); 349for (let pair of pairs) { 350 console.log(pair[0] + ', ' + pair[1]); 351} 352``` 353 354 355### tostring<sup>9+</sup> 356 357toString(): string 358 359Obtains search parameters that are serialized as a string and, if necessary, percent-encodes the characters in the string. 360 361**System capability**: SystemCapability.Utils.Lang 362 363**Return value** 364 365| Type| Description| 366| -------- | -------- | 367| string | String of serialized search parameters, which is percent-encoded if necessary.| 368 369**Example** 370 371```ts 372let url = Url.URL.parseURL('https://developer.exampleUrl/?fod=1&bard=2'); 373let params = new Url.URLParams(url.search.slice(1)); 374params.append('fod', '3'); 375console.log(params.toString()); 376``` 377 378## URL 379 380Provides APIs for parsing, constructing, and encoding URL strings. 381 382### Attributes 383 384**System capability**: SystemCapability.Utils.Lang 385 386| Name| Type| Readable| Writable| Description| 387| -------- | -------- | -------- | -------- | -------- | 388| hash | string | Yes| Yes| String that contains a harsh mark (#) followed by the fragment identifier of a URL.| 389| host | string | Yes| Yes| Host information in a URL.| 390| hostname | string | Yes| Yes| Hostname (without the port) in a URL.| 391| href | string | Yes| Yes| String that contains the whole URL.| 392| origin | string | Yes| No| Read-only string that contains the Unicode serialization of the origin of the represented URL.| 393| password | string | Yes| Yes| Password in a URL.| 394| pathname | string | Yes| Yes| Path in a URL.| 395| port | string | Yes| Yes| Port in a URL.| 396| protocol | string | Yes| Yes| Protocol in a URL.| 397| search | string | Yes| Yes| Serialized query string in a URL.| 398| searchParams<sup>(deprecated)</sup> | [URLSearchParams](#urlsearchparamsdeprecated) | Yes| No| **URLSearchParams** object allowing access to the query parameters in a URL.<br>- **NOTE**: This attribute is supported since API version 7 and is deprecated since API version 9. You are advised to use params<sup>9+</sup> instead.| 399| params<sup>9+</sup> | [URLParams](#urlparams9) | Yes| No| **URLParams** object allowing access to the query parameters in a URL.| 400| username | string | Yes| Yes| Username in a URL.| 401 402### constructor<sup>(deprecated)</sup> 403 404> **NOTE** 405> 406> This API is supported since API version 7 and deprecated since API version 9. You are advised to use [parseURL<sup>9+</sup>](#parseurl9) instead. 407 408constructor(url: string, base?: string | URL) 409 410Creates a URL. 411 412**System capability**: SystemCapability.Utils.Lang 413 414**Parameters** 415 416| Name| Type| Mandatory| Description| 417| -------- | -------- | -------- | -------- | 418| url | string | Yes| Input object.| 419| base | string \| URL | No| Input parameter, which can be any of the following:<br>- **string**: string<br>- **URL**: string or object<br>The default value is an empty string or an empty object.| 420 421**Example** 422 423```ts 424let mm = 'https://username:password@host:8080'; 425let a = new Url.URL("/", mm); // Output 'https://username:password@host:8080/'; 426let b = new Url.URL(mm); // Output 'https://username:password@host:8080/'; 427new Url.URL('path/path1', b); // Output 'https://username:password@host:8080/path/path1'; 428let c = new Url.URL('/path/path1', b); // Output 'https://username:password@host:8080/path/path1'; 429new Url.URL('/path/path1', c); // Output 'https://username:password@host:8080/path/path1'; 430new Url.URL('/path/path1', a); // Output 'https://username:password@host:8080/path/path1'; 431new Url.URL('/path/path1', "https://www.exampleUrl/fr-FR/toto"); // Output https://www.exampleUrl/path/path1 432new Url.URL('/path/path1', ''); // Raises a TypeError exception as '' is not a valid URL 433new Url.URL('/path/path1'); // Raises a TypeError exception as '/path/path1' is not a valid URL 434new Url.URL('https://www.example.com', ); // Output https://www.example.com/ 435new Url.URL('https://www.example.com', b); // Output https://www.example.com/ 436``` 437 438### constructor<sup>9+</sup> 439 440constructor() 441 442A no-argument constructor used to create a URL. It returns a **URL** object after **parseURL** is called. It is not used independently. 443 444**System capability**: SystemCapability.Utils.Lang 445 446### parseURL<sup>9+</sup> 447 448static parseURL(url : string, base?: string | URL): URL 449 450Parses a URL. 451 452**System capability**: SystemCapability.Utils.Lang 453 454**Parameters** 455 456| Name| Type| Mandatory| Description| 457| -------- | -------- | -------- | -------- | 458| url | string | Yes| Input object.| 459| base | string \| URL | No| Input parameter, which can be any of the following:<br>- **string**: string<br>- **URL**: string or object<br>The default value is an empty string or an empty object.| 460 461**Error codes** 462 463For details about the error codes, see [Utils Error Codes](../errorcodes/errorcode-utils.md). 464 465| ID| Error Message| 466| -------- | -------- | 467| 10200002 | Invalid url string. | 468 469 470**Example** 471 472```ts 473let mm = 'https://username:password@host:8080'; 474let url = Url.URL.parseURL(mm); 475let result = url.toString(); // Output 'https://username:password@host:8080/' 476``` 477 478### tostring 479 480toString(): string 481 482Converts the parsed URL into a string. 483 484**System capability**: SystemCapability.Utils.Lang 485 486**Return value** 487 488| Type| Description| 489| -------- | -------- | 490| string | Website address in a serialized string.| 491 492**Example** 493 494```ts 495const url = Url.URL.parseURL('https://username:password@host:8080/directory/file?query=pppppp#qwer=da'); 496let result = url.toString(); 497``` 498 499### toJSON 500 501toJSON(): string 502 503Converts the parsed URL into a JSON string. 504 505**System capability**: SystemCapability.Utils.Lang 506 507**Return value** 508 509| Type| Description| 510| -------- | -------- | 511| string | Website address in a serialized string.| 512 513**Example** 514```ts 515const url = Url.URL.parseURL('https://username:password@host:8080/directory/file?query=pppppp#qwer=da'); 516let result = url.toJSON(); 517``` 518 519## URLSearchParams<sup>(deprecated)</sup> 520 521Defines APIs for handling URL query strings. 522 523This class is deprecated since API version 9. You are advised to use [URLParams](#urlparams9) instead. 524 525### constructor<sup>(deprecated)</sup> 526 527constructor(init?: string[][] | Record<string, string> | string | URLSearchParams) 528 529A constructor used to create a **URLSearchParams** instance. 530 531> **NOTE** 532> 533> This API is supported since API version 7 and deprecated since API version 9. You are advised to use [URLParams.constructor<sup>9+</sup>](#constructor9) instead. 534 535**System capability**: SystemCapability.Utils.Lang 536 537**Parameters** 538 539| Name| Type| Mandatory| Description| 540| -------- | -------- | -------- | -------- | 541| init | string[][] \| Record<string, string> \| string \| URLSearchParams | No| Input parameter objects, which include the following:<br>- **string[][]**: two-dimensional string array<br>- **Record<string, string>**: list of objects<br>- **string**: string<br>- **URLSearchParams**: object<br>The default value is **null**.| 542 543**Example** 544 545```ts 546let objectParams = new Url.URLSearchParams([ ['user1', 'abc1'], ['query2', 'first2'], ['query3', 'second3'] ]); 547let objectParams1 = new Url.URLSearchParams({"fod" : '1' , "bard" : '2'}); 548let objectParams2 = new Url.URLSearchParams('?fod=1&bard=2'); 549let urlObject = new Url.URL('https://developer.mozilla.org/?fod=1&bard=2'); 550let params = new Url.URLSearchParams(urlObject.search); 551``` 552 553### append<sup>(deprecated)</sup> 554 555append(name: string, value: string): void 556 557Appends a key-value pair into the query string. 558 559> **NOTE** 560> 561> This API is supported since API version 7 and deprecated since API version 9. You are advised to use [URLParams.append<sup>9+</sup>](#append9) instead. 562 563**System capability**: SystemCapability.Utils.Lang 564 565**Parameters** 566 567| Name| Type| Mandatory| Description| 568| -------- | -------- | -------- | -------- | 569| name | string | Yes| Key of the key-value pair to append.| 570| value | string | Yes| Value of the key-value pair to append.| 571 572**Example** 573 574```ts 575let urlObject = new Url.URL('https://developer.exampleUrl/?fod=1&bard=2'); 576let paramsObject = new Url.URLSearchParams(urlObject.search.slice(1)); 577paramsObject.append('fod', '3'); 578``` 579 580### delete<sup>(deprecated)</sup> 581 582delete(name: string): void 583 584Deletes key-value pairs of the specified key. 585 586> **NOTE** 587> 588> This API is supported since API version 7 and deprecated since API version 9. You are advised to use [URLParams.delete<sup>9+</sup>](#delete9) instead. 589 590**System capability**: SystemCapability.Utils.Lang 591 592**Parameters** 593 594| Name| Type| Mandatory| Description| 595| -------- | -------- | -------- | -------- | 596| name | string | Yes| Key of the key-value pairs to delete.| 597 598**Example** 599 600```ts 601let urlObject = new Url.URL('https://developer.exampleUrl/?fod=1&bard=2'); 602let paramsobject = new Url.URLSearchParams(urlObject.search.slice(1)); 603paramsobject.delete('fod'); 604``` 605 606### getAll<sup>(deprecated)</sup> 607 608getAll(name: string): string[] 609 610Obtains all the key-value pairs based on the specified key. 611 612> **NOTE** 613> 614> This API is supported since API version 7 and deprecated since API version 9. You are advised to use [URLParams.getAll<sup>9+</sup>](#getall9) instead. 615 616**System capability**: SystemCapability.Utils.Lang 617 618**Parameters** 619 620| Name| Type| Mandatory| Description| 621| -------- | -------- | -------- | -------- | 622| name | string | Yes| Target key.| 623 624**Return value** 625 626| Type| Description| 627| -------- | -------- | 628| string[] | All key-value pairs matching the specified key.| 629 630**Example** 631 632```ts 633let urlObject = new Url.URL('https://developer.exampleUrl/?fod=1&bard=2'); 634let params = new Url.URLSearchParams(urlObject.search.slice(1)); 635params.append('fod', '3'); // Add a second value for the fod parameter. 636console.log(params.getAll('fod').toString()) // Output ["1","3"]. 637``` 638 639### entries<sup>(deprecated)</sup> 640 641entries(): IterableIterator<[string, string]> 642 643Obtains an ES6 iterator. Each item of the iterator is a JavaScript array, and the first and second fields of each array are the key and value respectively. 644 645> **NOTE** 646> 647> This API is supported since API version 7 and deprecated since API version 9. You are advised to use [URLParams.entries<sup>9+</sup>](#entries9) instead. 648 649**System capability**: SystemCapability.Utils.Lang 650 651**Return value** 652 653| Type| Description| 654| -------- | -------- | 655| IterableIterator<[string, string]> | ES6 iterator.| 656 657**Example** 658 659```ts 660let searchParamsObject = new Url.URLSearchParams("keyName1=valueName1&keyName2=valueName2"); 661let iter: Iterable<Object[]> = searchParamsObject.entries(); 662let pairs = Array.from(iter); 663for (let pair of pairs) { // Show keyName/valueName pairs 664 console.log(pair[0]+ ', '+ pair[1]); 665} 666``` 667 668 669### forEach<sup>(deprecated)</sup> 670 671forEach(callbackFn: (value: string, key: string, searchParams: URLSearchParams) => void, thisArg?: Object): void 672 673Traverses the key-value pairs in the **URLSearchParams** instance by using a callback. 674 675> **NOTE** 676> 677> This API is supported since API version 7 and deprecated since API version 9. You are advised to use [URLParams.forEach<sup>9+</sup>](#foreach9) instead. 678 679**System capability**: SystemCapability.Utils.Lang 680 681**Parameters** 682 683| Name| Type| Mandatory| Description| 684| -------- | -------- | -------- | -------- | 685| callbackFn | function | Yes| Callback invoked to traverse the key-value pairs in the **URLSearchParams** instance.| 686| thisArg | Object | No| Value of **this** to use when **callbackFn** is invoked. The default value is this object.| 687 688**Table 1** callbackFn parameter description 689 690| Name| Type| Mandatory| Description| 691| -------- | -------- | -------- | -------- | 692| value | string | Yes| Value that is currently traversed.| 693| key | string | Yes| Key that is currently traversed.| 694| searchParams | [URLSearchParams](#urlsearchparamsdeprecated) | Yes| Instance that invokes the **forEach** method.| 695 696**Example** 697 698```ts 699const myURLObject = new Url.URL('https://developer.exampleUrl/?fod=1&bard=2'); 700myURLObject.searchParams.forEach((value, name, searchParams) => { 701 console.log(name, value, myURLObject.searchParams === searchParams); 702}); 703``` 704 705 706### get<sup>(deprecated)</sup> 707 708get(name: string): string | null 709 710Obtains the value of the first key-value pair based on the specified key. 711 712> **NOTE** 713> 714> This API is supported since API version 7 and deprecated since API version 9. You are advised to use [URLParams.get<sup>9+</sup>](#get9) instead. 715 716**System capability**: SystemCapability.Utils.Lang 717 718**Parameters** 719 720| Name| Type| Mandatory| Description| 721| -------- | -------- | -------- | -------- | 722| name | string | Yes| Key specified to obtain the value.| 723 724**Return value** 725 726| Type| Description| 727| -------- | -------- | 728| string | Returns the value of the first key-value pair if obtained.| 729| null | Returns **null** if no value is obtained.| 730 731**Example** 732 733```ts 734let paramsObject = new Url.URLSearchParams('name=Jonathan&age=18'); 735let name = paramsObject.get("name"); // is the string "Jonathan" 736let age = paramsObject.get("age"); // is the string '18' 737``` 738 739 740### has<sup>(deprecated)</sup> 741 742has(name: string): boolean 743 744Checks whether a key has a value. 745 746> **NOTE** 747> 748> This API is supported since API version 7 and deprecated since API version 9. You are advised to use [URLParams.has<sup>9+</sup>](#has9) instead. 749 750**System capability**: SystemCapability.Utils.Lang 751 752**Parameters** 753 754| Name| Type| Mandatory| Description| 755| -------- | -------- | -------- | -------- | 756| name | string | Yes| Key specified to search for its value.| 757 758**Return value** 759 760| Type| Description| 761| -------- | -------- | 762| boolean | Returns **true** if the value exists; returns **false** otherwise.| 763 764**Example** 765 766```ts 767let urlObject = new Url.URL('https://developer.exampleUrl/?fod=1&bard=2'); 768let paramsObject = new Url.URLSearchParams(urlObject.search.slice(1)); 769paramsObject.has('bard') === true; 770``` 771 772 773### set<sup>(deprecated)</sup> 774 775set(name: string, value: string): void 776 777Sets the value for a key. If key-value pairs matching the specified key exist, the value of the first key-value pair will be set to the specified value and other key-value pairs will be deleted. Otherwise, the key-value pair will be appended to the query string. 778 779> **NOTE** 780> 781> This API is supported since API version 7 and deprecated since API version 9. You are advised to use [URLParams.set<sup>9+</sup>](#set9) instead. 782 783**System capability**: SystemCapability.Utils.Lang 784 785**Parameters** 786 787| Name| Type| Mandatory| Description| 788| -------- | -------- | -------- | -------- | 789| name | string | Yes| Key of the value to set.| 790| value | string | Yes| Value to set.| 791 792**Example** 793 794```ts 795let urlObject = new Url.URL('https://developer.exampleUrl/?fod=1&bard=2'); 796let paramsObject = new Url.URLSearchParams(urlObject.search.slice(1)); 797paramsObject.set('baz', '3'); // Add a third parameter. 798``` 799 800 801### sort<sup>(deprecated)</sup> 802 803sort(): void 804 805Sorts all key-value pairs contained in this object based on the Unicode code points of the keys and returns undefined. This method uses a stable sorting algorithm, that is, the relative order between key-value pairs with equal keys is retained. 806 807> **NOTE** 808> 809> This API is supported since API version 7 and deprecated since API version 9. You are advised to use [URLParams.sort<sup>9+</sup>](#sort9) instead. 810 811**System capability**: SystemCapability.Utils.Lang 812 813**Example** 814 815```ts 816let searchParamsObject = new Url.URLSearchParams("c=3&a=9&b=4&d=2"); // Create a test URLSearchParams object 817searchParamsObject.sort(); // Sort the key/value pairs 818console.log(searchParamsObject.toString()); // Display the sorted query string // Output a=9&b=4&c=3&d=2 819``` 820 821 822### keys<sup>(deprecated)</sup> 823 824keys(): IterableIterator<string> 825 826Obtains an ES6 iterator that contains the keys of all the key-value pairs. 827 828> **NOTE** 829> 830> This API is supported since API version 7 and deprecated since API version 9. You are advised to use [URLParams.keys<sup>9+</sup>](#keys9) instead. 831 832**System capability**: SystemCapability.Utils.Lang 833 834**Return value** 835 836| Type| Description| 837| -------- | -------- | 838| IterableIterator<string> | ES6 iterator that contains the keys of all the key-value pairs.| 839 840**Example** 841 842```ts 843let searchParamsObject = new Url.URLSearchParams("key1=value1&key2=value2"); // Create a URLSearchParamsObject object for testing 844let keys = Array.from(searchParamsObject.keys()); 845for (let key of keys) { // Output key-value pairs 846 console.log(key); 847} 848``` 849 850 851### values<sup>(deprecated)</sup> 852 853values(): IterableIterator<string> 854 855Obtains an ES6 iterator that contains the values of all the key-value pairs. 856 857> **NOTE** 858> 859> This API is supported since API version 7 and deprecated since API version 9. You are advised to use [URLParams.values<sup>9+</sup>](#values9) instead. 860 861**System capability**: SystemCapability.Utils.Lang 862 863**Return value** 864 865| Type| Description| 866| -------- | -------- | 867| IterableIterator<string> | ES6 iterator that contains the values of all the key-value pairs.| 868 869**Example** 870 871```ts 872let searchParams = new Url.URLSearchParams("key1=value1&key2=value2"); // Create a URLSearchParamsObject object for testing 873let values = Array.from(searchParams.values()); 874for (let value of values) { 875 console.log(value); 876} 877``` 878 879 880### [Symbol.iterator]<sup>(deprecated)</sup> 881 882[Symbol.iterator]\(): IterableIterator<[string, string]> 883 884Obtains an ES6 iterator. Each item of the iterator is a JavaScript array, and the first and second fields of each array are the key and value respectively. 885 886> **NOTE** 887> 888> This API is supported since API version 7 and deprecated since API version 9. You are advised to use [URLParams.[Symbol.iterator]<sup>9+</sup>](#symboliterator9) instead. 889 890**System capability**: SystemCapability.Utils.Lang 891 892**Return value** 893 894| Type| Description| 895| -------- | -------- | 896| IterableIterator<[string, string]> | ES6 iterator.| 897 898**Example** 899 900```ts 901const paramsObject = new Url.URLSearchParams('fod=bay&edg=bap'); 902let iter: Iterable<Object[]> = paramsObject[Symbol.iterator](); 903let pairs = Array.from(iter); 904for (let pair of pairs) { 905 console.log(pair[0] + ', ' + pair[1]); 906} 907``` 908 909### tostring<sup>(deprecated)</sup> 910 911toString(): string 912 913Obtains search parameters that are serialized as a string and, if necessary, percent-encodes the characters in the string. 914 915> **NOTE** 916> 917> This API is supported since API version 7 and deprecated since API version 9. You are advised to use [URLParams.tostring<sup>9+</sup>](#tostring9) instead. 918 919**System capability**: SystemCapability.Utils.Lang 920 921**Return value** 922 923| Type| Description| 924| -------- | -------- | 925| string | String of serialized search parameters, which is percent-encoded if necessary.| 926 927**Example** 928 929```ts 930let url = new Url.URL('https://developer.exampleUrl/?fod=1&bard=2'); 931let params = new Url.URLSearchParams(url.search.slice(1)); 932params.append('fod', '3'); 933console.log(params.toString()); 934``` 935 936