'foo'
"bar"
Hexadecimal转义序列
'\xA9' // "©"
Unicode转义序列
'\u00A9' // "©"
Unicode code point escape
'\u{2F804}'
// the same with simple Unicode escapes
'\uD87E\uDC04'
字符串对象是对原始字符串数据类型的封装
const foo = new String('foo'); // Creates a String object
console.log(foo); // Displays: [String: 'foo']
typeof foo; // Returns 'object'
const firstString = '2 + 2'; // Creates a string literal value
const secondString = new String('2 + 2'); // Creates a String object
eval(firstString); // Returns the number 4
eval(secondString); // Returns the string "2 + 2"
const hello = 'Hello, World!';
const helloLength = hello.length;
hello[0] = 'L'; // This has no effect, because strings are immutable
hello[0]; // This returns "H"
字符串对象的方法
charAt, charCodeAt, codePointAt | 返回索引指定位置的字符或者字符编码 |
indexOf, lastIndexOf | 返回指定子字符串初次出现的位置,或者最后一次出现的位置 |
startsWith, endsWith, includes | 判断是否以某指定文字起始、结尾、或者包含某指定文字 |
concat | 连接两个字符串,以新字符串返回 |
fromCharCode, fromCodePoint | 基于Unicode值创建字符串 |
split | 依据指定的分割符,将字符串分割写入数组 |
slice | 将字符串切割为片段,返回新字符串 |
subString, subStr | 依据指定索引返回子字符串 |
match, replace, search | 同正则表达式一起工作 |
toLowerCase, toUpperCase | 大小写转换 |
normalize | 返回字符串的Unicode Normalization Form |
repeat | 取得该字符串指定重复次数的字符串 |
trim | 除去字符串起始和结尾的空白 |
模板文字允许表达式嵌套在其中,模板文字使用回剔符号包围(``)
多行
console.log('string text line 1\n\
string text line 2');
// "string text line 1
// string text line 2"
console.log(`string text line 1
string text line 2`);
// "string text line 1
// string text line 2"
嵌套表达式
const five = 5;
const ten = 10;
console.log('Fifteen is ' + (five + ten) + ' and not ' + (2 * five + ten) + '.');
// "Fifteen is 15 and not 20."
const five = 5;
const ten = 10;
console.log(`Fifteen is ${five + ten} and not ${2 * five + ten}.`);
// "Fifteen is 15 and not 20."
Intl对象是ECMAScript Internationalization API
const msPerDay = 24 * 60 * 60 * 1000;
// July 17, 2014 00:00:00 UTC.
const july172014 = new Date(msPerDay * (44 * 365 + 11 + 197));
const options = { year: '2-digit', month: '2-digit', day: '2-digit',
hour: '2-digit', minute: '2-digit', timeZoneName: 'short' };
const americanDateTime = new Intl.DateTimeFormat('en-US', options).format;
console.log(americanDateTime(july172014)); // 07/16/14, 5:00 PM PDT
const gasPrice = new Intl.NumberFormat('en-US',
{ style: 'currency', currency: 'USD',
minimumFractionDigits: 3 });
console.log(gasPrice.format(5.259)); // $5.259
const hanDecimalRMBInChina = new Intl.NumberFormat('zh-CN-u-nu-hanidec',
{ style: 'currency', currency: 'CNY' });
console.log(hanDecimalRMBInChina.format(1314.25)); // ¥ 一,三一四.二五
Collator对象用来比较和排序字符串
const names = ['Hochberg', 'Hönigswald', 'Holzman'];
const germanPhonebook = new Intl.Collator('de-DE-u-co-phonebk');
// as if sorting ["Hochberg", "Hoenigswald", "Holzman"]:
console.log(names.sort(germanPhonebook.compare).join(', '));
// logs "Hochberg, Hönigswald, Holzman"
const germanDictionary = new Intl.Collator('de-DE-u-co-dict');
// as if sorting ["Hochberg", "Honigswald", "Holzman"]:
console.log(names.sort(germanDictionary.compare).join(', '));
// logs "Hochberg, Holzman, Hönigswald"