ES6 JavaScript 字符串详解

1、说明

/*
1、将一个值用单引号('')或者双引号("")引用起来就是字符串
2、ES6提供了反引号(``)定义一个字符串,可以支持多行,还支持插值和插值运算
*/

2、示例

let a = 'abc';
let b = "123";
let c = `${a+b}`;
let d = `
First Line
Second Line
Third Line
`;
let name = 'Tom', age = 19

console.log(1, a, b, c);
console.log(2, d);
console.log(3, `Hi, my name is ${name}. I am ${age}.`);
Info: Start process (上午11:33:17)
1 'abc' '123' 'abc123'
2 '\nFirst Line\nSecond Line\nThird Line\n'
3 'Hi, my name is Tom. I am 19.'
Info: End process (上午11:33:17)

3、字符串操作方法

let family = 'Luna-Sybil-Tang'

console.log(1, family.charAt(0), family.charAt(9)) 
console.log(2, family[0], family[9])
console.log(3, family.toUpperCase())
console.log(4, family.toLowerCase())
console.log(5, family.slice(0, 2))
console.log(6, family.slice(-1))
console.log(7, family.slice(-2))
console.log(8, family.slice(-2, -1))
console.log(9, family.split('-'))
console.log(10, family.substr(1, 3))     // 返回字串,从何处开始,取多长
console.log(11, family.substring(1, 3))  // 返回字串,从何处开始,到何处结束
console.log(12, family.sub())
console.log(13, family.indexOf('a'))     // 从索引N查找字符串字串,默认从零开始,超限后返回 -1
console.log(14, family.indexOf('a', 6))
console.log(15, family.indexOf('a', 22)) 
console.log(16, family.replace('Tang', 'Neo'))
console.log(17, family.repeat(3))
console.log(18, family.concat('-Neo'))   // 连接
Info: Start process (下午12:24:32)
1 'L' 'l'
2 'L' 'l'
3 'LUNA-SYBIL-TANG'
4 'luna-sybil-tang'
5 'Lu'
6 'g'
7 'ng'
8 'n'
9 [ 'Luna', 'Sybil', 'Tang' ]
10 'una'
11 'un'
12 '<sub>Luna-Sybil-Tang</sub>'
13 3
14 12
15 -1
16 'Luna-Sybil-Neo'
17 'Luna-Sybil-TangLuna-Sybil-TangLuna-Sybil-Tang'
18 'Luna-Sybil-Tang-Neo'
Info: End process (下午12:24:32)
let neo = ' \t Neo Tang \r\n'
console.log(1, neo)
console.log(2, neo.trim())         // 去掉两端的空白字符,非标函数,少用
console.log(3, neo.trimLeft())
console.log(4, neo.trimRight())
Info: Start process (下午12:25:31)
1 ' \t Neo Tang \r\n'
2 'Neo Tang'
3 'Neo Tang \r\n'
4 ' \t Neo Tang'
Info: End process (下午12:25:31)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值