字符串

字符串字面表达

\'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  
console.log(foo); // Displays: [String: \'foo\']
typeof foo; // Returns \' \'

const firstString = \'2 + 2\'; // Creates a string literal value
const secondString = new String(\'2 + 2\'); // Creates a String  
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 除去字符串起始和结尾的空白

 

收藏 打印