0
点赞
收藏
分享

微信扫一扫

JavaScript中关于字符串的操作方法

孟佳 2022-03-27 阅读 38

字符串的拼接:concat()

let string = 'hello'
//接收一个参数
let result = string.concat(',world')
//接收多个参数
let result1 = string.concat(',world',' !')
console.log(string);// hello
console.log(result);// hello,world
console.log(result1);// hello,world !
let result = 'hello'+',world'+'!'
console.log(result);//hello,world!

字符串的截取:slice(),substring(),substr()

let string = "hello world"; 
console.log(string.slice(3)); // "lo world" 
console.log(string.substring(3)); // "lo world" 
console.log(string.substr(3)); // "lo world" 
console.log(string.slice(3, 7)); // "lo w" 
console.log(string.substring(3,7)); // "lo w" 
console.log(string.substr(3, 7)); // "lo worl" 
let string = "hello world"; 
console.log(string.slice(-3)); // "rld" 
console.log(string.substring(-3)); // "hello world" 
console.log(string.substr(-3)); // "rld" 
console.log(string.slice(3, -4)); // "lo w" 
console.log(string.substring(3, -4)); // "hel" 
console.log(string.substr(3, -4)); // "" (empty string) 

举报

相关推荐

0 条评论