新的字串方法
> 'hello'.startsWith('hell')
true
> 'hello'.endsWith('ello')
true
> 'hello'.includes('ell')
true
> 'doo '.repeat(3)
'doo doo doo '
ES6 有一種新的字串字面值,稱為範本字面值
// String interpolation via template literals (in backticks)
const
first
=
'Jane'
;
const
last
=
'Doe'
;
console
.
log
(
`Hello
${
first
}
${
last
}
!`
);
// Hello Jane Doe!
// Template literals also let you create strings with multiple lines
const
multiLine
=
`
This is
a string
with multiple
lines`
;
在 ECMAScript 6 中,有一種新的 Unicode 跳脫,讓您可以指定任何碼點(甚至超過 16 位元)
console
.
log
(
'\u{1F680}'
);
// ES6: single code point
console
.
log
(
'\uD83D\uDE80'
);
// ES5: two code units
有關跳脫的更多資訊,請參閱Unicode 章節。
範本字面值在其專屬章節中有深入的說明。它們提供了三個有趣的特色。
首先,範本字面值支援字串插值
const
first
=
'Jane'
;
const
last
=
'Doe'
;
console
.
log
(
`Hello
${
first
}
${
last
}
!`
);
// Hello Jane Doe!
其次,範本字面值可以包含多行
const
multiLine
=
`
This is
a string
with multiple
lines`
;
第三,如果您在範本字面值前面加上標籤String.raw
,它們就是「原始」的,反斜線不是特殊字元,而且不會解釋跳脫,例如 \n
const
str
=
String
.
raw
`Not a newline:
\
n`
;
console
.
log
(
str
===
'Not a newline: \\n'
);
// true
字串是可迭代的,這表示您可以使用 for-of
來迭代它們的字元
for
(
const
ch
of
'abc'
)
{
console
.
log
(
ch
);
}
// Output:
// a
// b
// c
而且您可以使用展開運算子 (...
) 將字串轉換成陣列
const
chars
=
[...
'abc'
];
// ['a', 'b', 'c']
字串迭代器會沿著碼點邊界分割字串,這表示它傳回的字串包含一個或兩個 JavaScript 字元
for
(
const
ch
of
'x\uD83D\uDE80y'
)
{
console
.
log
(
ch
.
length
);
}
// Output:
// 1
// 2
// 1
反覆運算提供一種快速的方式來計算字串中的 Unicode 碼點
> [...'x\uD83D\uDE80y'].length
3
反覆運算也有助於反轉包含非 BMP 碼點(大於 16 位元,並編碼為兩個 JavaScript 字元)的字串
const
str
=
'x\uD83D\uDE80y'
;
// ES5: \uD83D\uDE80 are (incorrectly) reversed
console
.
log
(
str
.
split
(
''
).
reverse
().
join
(
''
));
// 'y\uDE80\uD83Dx'
// ES6: order of \uD83D\uDE80 is preserved
console
.
log
([...
str
].
reverse
().
join
(
''
));
// 'y\uD83D\uDE80x'
新的方法 codePointAt()
傳回字串中特定索引處碼點的數值
const
str
=
'x\uD83D\uDE80y'
;
console
.
log
(
str
.
codePointAt
(
0
).
toString
(
16
));
// 78
console
.
log
(
str
.
codePointAt
(
1
).
toString
(
16
));
// 1f680
console
.
log
(
str
.
codePointAt
(
3
).
toString
(
16
));
// 79
此方法與字串反覆運算結合使用時效果良好
for
(
const
ch
of
'x\uD83D\uDE80y'
)
{
console
.
log
(
ch
.
codePointAt
(
0
).
toString
(
16
));
}
// Output:
// 78
// 1f680
// 79
codePointAt()
的相反方法是 String.fromCodePoint()
> String.fromCodePoint(0x78, 0x1f680, 0x79) === 'x\uD83D\uDE80y'
true
三個新方法檢查一個字串是否存在於另一個字串中
> 'hello'.startsWith('hell')
true
> 'hello'.endsWith('ello')
true
> 'hello'.includes('ell')
true
這些方法中的每一個都有一個位置作為可選的第二個參數,指定要搜尋的字串從何處開始或結束
> 'hello'.startsWith('ello', 1)
true
> 'hello'.endsWith('hell', 4)
true
> 'hello'.includes('ell', 1)
true
> 'hello'.includes('ell', 2)
false
repeat()
方法重複字串
> 'doo '.repeat(3)
'doo doo doo '
在 ES6 中,接受正規表示式參數的四個字串方法所做的工作相對較少。它們主要呼叫參數的方法
String.prototype.match(regexp)
呼叫regexp[Symbol.match](this)
.String.prototype.replace(searchValue, replaceValue)
呼叫searchValue[Symbol.replace](this, replaceValue)
.String.prototype.search(regexp)
呼叫regexp[Symbol.search](this)
.String.prototype.split(分隔符號, 極限)
呼叫分隔符號[Symbol.split](此, 極限)
.參數不再必須是正規表示式。任何具有適當方法的物件都可以。
標記範本
String.raw(呼叫點, ...替換) : 字串
> String.raw`\n` === '\\n'
true
請參閱 範本字串章節 以取得更多資訊。
Unicode 和碼點
String.fromCodePoint(...碼點 : 數字[]) : 字串
String.prototype.codePointAt(位置) : 數字
pos
開始的碼點數字(包含一個或兩個 JavaScript 字元)。String.prototype.normalize(格式? : 字串) : 字串
'NFC'
格式。尋找字串
String.prototype.startsWith(搜尋字串, 位置=0) : 布林值
搜尋字串
開頭?位置
讓您可以指定要檢查的字串從何處開始。String.prototype.endsWith(搜尋字串, 結束位置=搜尋字串.長度) : 布林值
搜尋字串
結尾?結束位置
讓您可以指定要檢查的字串從何處結束。String.prototype.includes(搜尋字串, 位置=0) : 布林值
搜尋字串
?位置
讓您可以指定要搜尋的字串從何處開始。重複字串
String.prototype.repeat(次數) : 字串
次數
次。