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