js-apis-uri.md 3.5 KB
Newer Older
W
wusongqing 已提交
1
# URI String Parsing
Z
zengyawen 已提交
2

W
wusongqing 已提交
3 4
> **NOTE**
>
W
wusongqing 已提交
5
> 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.
Z
zengyawen 已提交
6

W
wusongqing 已提交
7 8

## Modules to Import
Z
zengyawen 已提交
9

W
wusongqing 已提交
10
```js
Z
zengyawen 已提交
11 12 13
import uri from '@ohos.uri'  
```

W
wusongqing 已提交
14 15 16 17
## URI

### Attributes

W
wusongqing 已提交
18 19
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
| Name| Type| Readable| Writable| Description|
| -------- | -------- | -------- | -------- | -------- |
| scheme | string | Yes| No| Scheme in the URI.|
| userInfo | string | Yes| No| User information in the URI.|
| host | string | Yes| No| Host name (without the port number) in the URI.|
| port | string | Yes| No| Port number in the URI.|
| path | string | Yes| No| Path in the URI.|
| query | string | Yes| No| Query part in the URI.|
| fragment | string | Yes| No| Fragment part in the URI.|
| authority | string | Yes| No| Authority part in the URI.|
| ssp | string | Yes| No| Scheme-specific part in the URI.|


### constructor

constructor(uri: string)
Z
zengyawen 已提交
36 37 38

A constructor used to create a URI instance.

W
wusongqing 已提交
39 40
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
41
**Parameters**
Z
zengyawen 已提交
42

W
wusongqing 已提交
43 44
| Name| Type.| Readable| Writable| Description|
| -------- | -------- | -------- | -------- | -------- |
W
wusongqing 已提交
45
| uri | string | Yes| Yes| Input object.|
Z
zengyawen 已提交
46

W
wusongqing 已提交
47
**Example**
Z
zengyawen 已提交
48

49
```js
W
wusongqing 已提交
50 51 52
var mm = 'http://username:password@host:8080/directory/file?foo=1&bar=2#fragment';
new uri.URI(mm); // Output 'http://username:password@host:8080/directory/file?foo=1&bar=2#fragment';
```
53
```js
W
wusongqing 已提交
54 55 56 57 58
new uri.URI('http://username:password@host:8080'); // Output 'http://username:password@host:8080';
```


### toString
Z
zengyawen 已提交
59

W
wusongqing 已提交
60
toString(): string
Z
zengyawen 已提交
61

W
wusongqing 已提交
62 63
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
64
Obtains the query string applicable to this URI.
W
wusongqing 已提交
65 66 67 68 69 70

**Return value**

| Type.| Description|
| -------- | -------- |
| string | Website address in a serialized string.|
Z
zengyawen 已提交
71

W
wusongqing 已提交
72
**Example**
Z
zengyawen 已提交
73

74
```js
W
wusongqing 已提交
75 76
const uri = new uri.URI('http://username:password@host:8080/directory/file?query=pppppp#qwer=da');
uri.toString()
W
wusongqing 已提交
77
```
Z
zengyawen 已提交
78 79


W
wusongqing 已提交
80 81 82
### equals

equals(other: URI): boolean
Z
zengyawen 已提交
83 84 85

Checks whether this URI is the same as another URI object.

W
wusongqing 已提交
86 87
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
88
**Parameters**
Z
zengyawen 已提交
89

W
wusongqing 已提交
90 91 92 93 94 95 96 97 98 99 100 101
| Name| Type.| Mandatory| Description|
| -------- | -------- | -------- | -------- |
| other | [URI](#uri) | Yes| URI object to compare.|

**Return value**

| Type.| Description|
| -------- | -------- |
| boolean | Returns **true** if the two URIs are the same; returns **false** otherwise.|

**Example**

102
```js
W
wusongqing 已提交
103 104 105 106 107 108 109 110
const uriInstance = new uri.URI('http://username:password@host:8080/directory/file?query=pppppp#qwer=da');
const uriInstance1 = new uri.URI('http://username:password@host:8080/directory/file?query=pppppp#qwer=da#fragment');
uriInstance.equals(uriInstance1);
```

### checkIsAbsolute

checkIsAbsolute(): boolean
Z
zengyawen 已提交
111

W
wusongqing 已提交
112
Checks whether this URI is an absolute URI (whether the scheme component is defined).
Z
zengyawen 已提交
113

W
wusongqing 已提交
114 115
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
116
**Return value**
Z
zengyawen 已提交
117

W
wusongqing 已提交
118 119 120
| Type.| Description|
| -------- | -------- |
| boolean | Returns **true** if the URI is an absolute URI; returns **false** otherwise.|
Z
zengyawen 已提交
121

W
wusongqing 已提交
122
**Example**
Z
zengyawen 已提交
123

124
```js
W
wusongqing 已提交
125 126 127 128 129 130 131 132
const uriInstance = new uri.URI('http://username:password@www.qwer.com:8080?query=pppppp');
uriInstance.checkIsAbsolute();
```


### normalize

normalize(): URI
Z
zengyawen 已提交
133

W
wusongqing 已提交
134 135
Normalizes the path of this URI.

W
wusongqing 已提交
136 137
**System capability**: SystemCapability.Utils.Lang

W
wusongqing 已提交
138 139 140 141 142
**Return value**

| Type.| Description|
| -------- | -------- |
| URI | URI with the normalized path.|
Z
zengyawen 已提交
143

W
wusongqing 已提交
144
**Example**
145
```js
W
wusongqing 已提交
146 147 148 149
const uriInstance = new uri.URI('http://username:password@www.qwer.com:8080/path/path1/../path2/./path3?query=pppppp');
let uriInstance1 = uriInstance.normalize();
uriInstance1.path;
```