string.md 12.3 KB
Newer Older
D
DCloud_LXH 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296
# String

String 全局对象是一个用于字符串或一个字符序列的构造函数。

字符串字面量采取以下形式:

```ts
'string text'
"string text"
"中文/汉语"
"español"
"English "
"हिन्दी"
"العربية"
"português"
"বাংলা"
"русский"
"日本語"
"ਪੰਜਾਬੀ"
"한국어"
```

## 实例属性


### length

<!-- UTSJSON.String.length.description -->

<!-- UTSJSON.String.length.param -->

<!-- UTSJSON.String.length.returnValue -->

```ts
const x = "Mozilla";
const empty = "";

console.log("Mozilla is " + x.length + " code units long");
/* "Mozilla is 7 code units long" */

console.log("The empty string is has a length of " + empty.length);
/* "The empty string is has a length of 0" */
```

<!-- UTSJSON.String.length.compatibility -->


## 实例方法

### includes(searchString, position?)

<!-- UTSJSON.String.includes.description -->

<!-- UTSJSON.String.includes.param -->

<!-- UTSJSON.String.includes.returnValue -->

```ts
const str = 'To be, or not to be, that is the question.';

console.log(str.includes('To be'));       // true
console.log(str.includes('question'));    // true
console.log(str.includes('nonexistent')); // false
console.log(str.includes('To be', 1));    // false
console.log(str.includes('TO BE'));       // false
```

<!-- UTSJSON.String.includes.compatibility -->

### endsWith(searchString, endPosition?)

<!-- UTSJSON.String.endsWith.description -->

<!-- UTSJSON.String.endsWith.param -->

<!-- UTSJSON.String.endsWith.returnValue -->

```ts
const str1 = 'Cats are the best!';
console.log(str1.endsWith('best!'));
// expected output: true
console.log(str1.endsWith('best', 17));
// expected output: true
const str2 = 'Is this a question?';
console.log(str2.endsWith('question'));
// expected output: false
```

<!-- UTSJSON.String.endsWith.compatibility -->

### repeat(count)

<!-- UTSJSON.String.repeat.description -->

<!-- UTSJSON.String.repeat.param -->

<!-- UTSJSON.String.repeat.returnValue -->

```ts
"abc".repeat(0)      // ""
"abc".repeat(1)      // "abc"
"abc".repeat(2)      // "abcabc"
"abc".repeat(3.5)    // "abcabcabc" 参数 count 将会被自动转换成整数。
```

<!-- UTSJSON.String.repeat.compatibility -->

### startsWith(searchString, position?)

<!-- UTSJSON.String.startsWith.description -->

<!-- UTSJSON.String.startsWith.param -->

<!-- UTSJSON.String.startsWith.returnValue -->

<!-- UTSJSON.String.startsWith.compatibility -->

### at(index)

<!-- UTSJSON.String.at.description -->

<!-- UTSJSON.String.at.param -->

<!-- UTSJSON.String.at.returnValue -->

```ts
const sentence = 'The quick brown fox jumps over the lazy dog.';
let index = 5;
console.log(`Using an index of ${index} the character returned is ${sentence.at(index)}`);
// expected output: "Using an index of 5 the character returned is u"
index = -4;
console.log(`Using an index of ${index} the character returned is ${sentence.at(index)}`);
// expected output: "Using an index of -4 the character returned is d"
```

<!-- UTSJSON.String.at.compatibility -->

### charAt(pos)

<!-- UTSJSON.String.charAt.description -->

<!-- UTSJSON.String.charAt.param -->

<!-- UTSJSON.String.charAt.returnValue -->

```ts
const anyString = "Brave new world";

console.log("The character at index 0   is '" + anyString.charAt(0)   + "'");
// The character at index 0 is 'B'
console.log("The character at index 1   is '" + anyString.charAt(1)   + "'");
// The character at index 1 is 'r'
console.log("The character at index 2   is '" + anyString.charAt(2)   + "'");
// The character at index 2 is 'a'
console.log("The character at index 3   is '" + anyString.charAt(3)   + "'");
// The character at index 3 is 'v'
console.log("The character at index 4   is '" + anyString.charAt(4)   + "'");
// The character at index 4 is 'e'
console.log("The character at index 999 is '" + anyString.charAt(999) + "'");
// The character at index 999 is ''
```

<!-- UTSJSON.String.charAt.compatibility -->

### charCodeAt(index)

<!-- UTSJSON.String.charCodeAt.description -->

<!-- UTSJSON.String.charCodeAt.param -->

<!-- UTSJSON.String.charCodeAt.returnValue -->

```ts
const sentence = 'The quick brown fox jumps over the lazy dog.';
const index = 4;
console.log(`The character code ${sentence.charCodeAt(index)} is equal to ${sentence.charAt(index)}`);
// expected output: "The character code 113 is equal to q"
```

<!-- UTSJSON.String.charCodeAt.compatibility -->

### concat(strings)

<!-- UTSJSON.String.concat.description -->

<!-- UTSJSON.String.concat.param -->

<!-- UTSJSON.String.concat.returnValue -->

```ts
let hello = 'Hello, '
console.log(hello.concat('Kevin', '. Have a nice day.'))
// Hello, Kevin. Have a nice day.
```

<!-- UTSJSON.String.concat.compatibility -->

### indexOf(searchString, position?)

<!-- UTSJSON.String.indexOf.description -->

<!-- UTSJSON.String.indexOf.param -->

<!-- UTSJSON.String.indexOf.returnValue -->

```ts
const paragraph = 'The quick brown fox jumps over the lazy dog. If the dog barked, was it really lazy?';

const searchTerm = 'dog';
const indexOfFirst = paragraph.indexOf(searchTerm);

console.log(`The index of the first "${searchTerm}" from the beginning is ${indexOfFirst}`);
// expected output: "The index of the first "dog" from the beginning is 40"

console.log(`The index of the 2nd "${searchTerm}" is ${paragraph.indexOf(searchTerm, (indexOfFirst + 1))}`);
// expected output: "The index of the 2nd "dog" is 52"

```

<!-- UTSJSON.String.indexOf.compatibility -->

### padEnd

padEnd() 方法会用一个字符串填充当前字符串(如果需要的话则重复填充),返回填充后达到指定长度的字符串。从当前字符串的末尾(右侧)开始填充。

**平台差异说明**

|JavaScript|Kotlin|Swift|
|:-:|:-:|:-:|
|√|√|√ `(3.6.11+)`|

```ts
const str1 = 'Breaded Mushrooms';
console.log(str1.padEnd(25, '.'));
// expected output: "Breaded Mushrooms........"
const str2 = '200';
console.log(str2.padEnd(5));
// expected output: "200  "
```
### padStart

padStart() 方法用另一个字符串填充当前字符串 (如果需要的话,会重复多次),以便产生的字符串达到给定的长度。从当前字符串的左侧开始填充。

**平台差异说明**

|JavaScript|Kotlin|Swift|
|:-:|:-:|:-:|
|√|√|√ `(3.6.11+)`|

```ts
const str1 = '5';
console.log(str1.padStart(2, '0'));
// expected output: "05"
```

### lastIndexOf(searchString, position?)

<!-- UTSJSON.String.lastIndexOf.description -->

<!-- UTSJSON.String.lastIndexOf.param -->

<!-- UTSJSON.String.lastIndexOf.returnValue -->

<!-- UTSJSON.String.lastIndexOf.compatibility -->

### replace(searchValue, replaceValue)

<!-- UTSJSON.String.replace.description -->

<!-- UTSJSON.String.replace.param -->

<!-- UTSJSON.String.replace.returnValue -->

```ts
const p = 'The quick brown fox jumps over the lazy dog. If the dog reacted, was it really lazy?';

console.log(p.replace('dog', 'monkey'));
// expected output: "The quick brown fox jumps over the lazy monkey. If the dog reacted, was it really lazy?"
const regex = /Dog/i;
console.log(p.replace(regex, 'ferret'));
// expected output: "The quick brown fox jumps over the lazy ferret. If the dog reacted, was it really lazy?"

```

<!-- UTSJSON.String.replace.compatibility -->

### replace(searchValue, replacer)

<!-- UTSJSON.String.replace_1.description -->

<!-- UTSJSON.String.replace_1.param -->

<!-- UTSJSON.String.replace_1.returnValue -->

<!-- UTSJSON.String.replace_1.compatibility -->

D
DCloud_LXH 已提交
297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322

```ts
// 不包含捕捉组的示例
let a = "The quick brown fox jumps over the lazy dog. If the dog reacted, was it really lazy?"
let b = a.replace(RegExp("fox"),function(match: string, offset: number, string: string):string{
    console.log("match",match)
    console.log("offset",offset)
    console.log("string",string)
    return "cat"
})
console.log("b:",b)

// 包含一个捕获组的示例。注意,目前android仅支持最多五个捕获组
let a1 = "The quick brown fox jumps over the lazy dog. If the dog reacted, was it really lazy?"
let b1 = a1.replace(RegExp("(fox)"),function(match: string,p1: string, offset: number, string: string):string{
    console.log("match",match)
    console.log("p1",p1)
    console.log("offset",offset)
    console.log("string",string)
    return "cat"
})
console.log("b1",b1)
```



D
DCloud_LXH 已提交
323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520
### search(regexp)

<!-- UTSJSON.String.search.description -->

<!-- UTSJSON.String.search.param -->

<!-- UTSJSON.String.search.returnValue -->

```ts
const paragraph = 'The quick brown fox jumps over the lazy dog. If the dog barked, was it really lazy?';
// any character that is not a word character or whitespace
const regex = /[^\w\s]/g;
console.log(paragraph.search(regex));
// expected output: 43
console.log(paragraph[paragraph.search(regex)]);
// expected output: "."
```

<!-- UTSJSON.String.search.compatibility -->

### slice(start?, end?)

<!-- UTSJSON.String.slice.description -->

<!-- UTSJSON.String.slice.param -->

<!-- UTSJSON.String.slice.returnValue -->

```ts
const str = 'The quick brown fox jumps over the lazy dog.';
console.log(str.slice(31));
// expected output: "the lazy dog."
console.log(str.slice(4, 19));
// expected output: "quick brown fox"
```

<!-- UTSJSON.String.slice.compatibility -->

### split(separator, limit?)

<!-- UTSJSON.String.split.description -->

<!-- UTSJSON.String.split.param -->

<!-- UTSJSON.String.split.returnValue -->

```ts
const str = 'The quick brown fox jumps over the lazy dog.';

const words = str.split(' ');
console.log(words[3]);
// expected output: "fox"
const chars = str.split('');
console.log(chars[8]);
// expected output: "k"
```

<!-- UTSJSON.String.split.compatibility -->

### substring(start, end?)

<!-- UTSJSON.String.substring.description -->

<!-- UTSJSON.String.substring.param -->

<!-- UTSJSON.String.substring.returnValue -->

<!-- UTSJSON.String.substring.compatibility -->


**bug&tips**

当前版本android平台 start 与 end 参数声明为Int, 需要将number 参数转换为int

```uts
let a = 1
let b = 2
// 临时解决办法
"hello uts".substring(a.toInt(),b.toInt())
```

这个问题后续会修复


### toLowerCase()

<!-- UTSJSON.String.toLowerCase.description -->

<!-- UTSJSON.String.toLowerCase.param -->

<!-- UTSJSON.String.toLowerCase.returnValue -->

```ts
console.log('中文简体 zh-CN || zh-Hans'.toLowerCase());
// 中文简体 zh-cn || zh-hans
console.log( "ALPHABET".toLowerCase() );
// "alphabet"
```

<!-- UTSJSON.String.toLowerCase.compatibility -->

### toUpperCase()

<!-- UTSJSON.String.toUpperCase.description -->

<!-- UTSJSON.String.toUpperCase.param -->

<!-- UTSJSON.String.toUpperCase.returnValue -->

```ts
const sentence = 'The quick brown fox jumps over the lazy dog.';
console.log(sentence.toUpperCase());
// expected output: "THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG."
```

<!-- UTSJSON.String.toUpperCase.compatibility -->

### trim()

<!-- UTSJSON.String.trim.description -->

<!-- UTSJSON.String.trim.param -->

<!-- UTSJSON.String.trim.returnValue -->

<!-- UTSJSON.String.trim.compatibility -->

### padStart(targetLength, padString?)

<!-- UTSJSON.String.padStart.description -->

<!-- UTSJSON.String.padStart.param -->

<!-- UTSJSON.String.padStart.returnValue -->

<!-- UTSJSON.String.padStart.compatibility -->

### padEnd(targetLength, padString?)

<!-- UTSJSON.String.padEnd.description -->

<!-- UTSJSON.String.padEnd.param -->

<!-- UTSJSON.String.padEnd.returnValue -->

<!-- UTSJSON.String.padEnd.compatibility -->

### includes(searchString, position?)

<!-- UTSJSON.String.includes.description -->

<!-- UTSJSON.String.includes.param -->

<!-- UTSJSON.String.includes.returnValue -->

<!-- UTSJSON.String.includes.compatibility -->

### endsWith(searchString, endPosition?)

<!-- UTSJSON.String.endsWith.description -->

<!-- UTSJSON.String.endsWith.param -->

<!-- UTSJSON.String.endsWith.returnValue -->

<!-- UTSJSON.String.endsWith.compatibility -->

### repeat(count)

<!-- UTSJSON.String.repeat.description -->

<!-- UTSJSON.String.repeat.param -->

<!-- UTSJSON.String.repeat.returnValue -->

<!-- UTSJSON.String.repeat.compatibility -->

### startsWith(searchString, position?)

<!-- UTSJSON.String.startsWith.description -->

<!-- UTSJSON.String.startsWith.param -->

<!-- UTSJSON.String.startsWith.returnValue -->

<!-- UTSJSON.String.startsWith.compatibility -->

### at(index)

<!-- UTSJSON.String.at.description -->

<!-- UTSJSON.String.at.param -->

<!-- UTSJSON.String.at.returnValue -->

<!-- UTSJSON.String.at.compatibility -->

<!-- UTSJSON.String.tutorial -->