提交 ade3eb9b 编写于 作者: fxy060608's avatar fxy060608

docs: update syntax-uts.md

上级 a30023ac
...@@ -1739,6 +1739,250 @@ forEach 方法会根据集合中元素的插入顺序,依次执行提供的回 ...@@ -1739,6 +1739,250 @@ forEach 方法会根据集合中元素的插入顺序,依次执行提供的回
has() 方法返回一个布尔值来指示对应的值 value 是否存在 Set 对象中。 has() 方法返回一个布尔值来指示对应的值 value 是否存在 Set 对象中。
### String
String 全局对象是一个用于字符串或一个字符序列的构造函数。
字符串字面量采取以下形式:
```ts
'string text'
"string text"
"中文/汉语"
"español"
"English "
"हिन्दी"
"العربية"
"português"
"বাংলা"
"русский"
"日本語"
"ਪੰਜਾਬੀ"
"한국어"
```
#### 实例属性
#### length
length 属性表示一个字符串的长度。
```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" */
```
#### 实例方法
#### at
at() 方法接受一个整数值,并返回一个新的 String,该字符串由位于指定偏移量处的单个 UTF-16 码元组成。该方法允许正整数和负整数。负整数从字符串中的最后一个字符开始倒数。
```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"
```
#### charAt
charAt() 方法从一个字符串中返回指定的字符。
```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 ''
```
#### charCodeAt
charCodeAt() 方法返回 0 到 65535 之间的整数,表示给定索引处的 UTF-16 代码单元
```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"
```
#### concat
concat() 方法将一个或多个字符串与原字符串连接合并,形成一个新的字符串并返回。
```ts
let hello = 'Hello, '
console.log(hello.concat('Kevin', '. Have a nice day.'))
// Hello, Kevin. Have a nice day.
```
#### endsWith
endsWith() 方法用来判断当前字符串是否是以另外一个给定的子字符串“结尾”的,根据判断结果返回 true 或 false。
```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
```
#### includes
includes() 方法用于判断一个字符串是否包含在另一个字符串中,根据情况返回 true 或 false。
```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
```
#### indexOf
indexOf() 方法返回调用它的 String 对象中第一次出现的指定值的索引,从 fromIndex 处进行搜索。如果未找到该值,则返回 -1。
```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"
```
#### padEnd
padEnd() 方法会用一个字符串填充当前字符串(如果需要的话则重复填充),返回填充后达到指定长度的字符串。从当前字符串的末尾(右侧)开始填充。
```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() 方法用另一个字符串填充当前字符串 (如果需要的话,会重复多次),以便产生的字符串达到给定的长度。从当前字符串的左侧开始填充。
```ts
const str1 = '5';
console.log(str1.padStart(2, '0'));
// expected output: "05"
```
#### repeat
repeat() 构造并返回一个新字符串,该字符串包含被连接在一起的指定数量的字符串的副本。
```ts
"abc".repeat(0) // ""
"abc".repeat(1) // "abc"
"abc".repeat(2) // "abcabc"
"abc".repeat(3.5) // "abcabcabc" 参数 count 将会被自动转换成整数。
```
#### replace
replace() 方法返回一个由替换值(replacement)替换部分或所有的模式(pattern)匹配项后的新字符串。模式可以是一个字符串或者一个正则表达式,替换值可以是一个字符串或者一个每次匹配都要调用的回调函数。如果pattern是字符串,则仅替换第一个匹配项。原字符串不会改变。
```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?"
```
#### search
search() 方法执行正则表达式和 String 对象之间的一个搜索匹配。
```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: "."
```
#### slice
slice() 方法提取某个字符串的一部分,并返回一个新的字符串,且不会改动原字符串。
```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"
```
#### split
split() 方法使用指定的分隔符字符串将一个String对象分割成子字符串数组,以一个指定的分割字串来决定每个拆分的位置。
```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"
```
#### toLowerCase
toLowerCase() 会将调用该方法的字符串值转为小写形式,并返回。
```ts
console.log('中文简体 zh-CN || zh-Hans'.toLowerCase());
// 中文简体 zh-cn || zh-hans
console.log( "ALPHABET".toLowerCase() );
// "alphabet"
```
#### toUpperCase
toUpperCase() 方法将调用该方法的字符串转为大写形式并返回(如果调用该方法的值不是字符串类型会被强制转换)。
```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."
```
### 定时器 ### 定时器
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册