网站的功能和作用是什么,哈尔滨网站备案,网页设计公司163企业邮箱,种子搜索器JavaScript中字符串是一种基本数据类型#xff0c;表示文本数据。字符串常用方法有以下几种#xff1a;
length#xff1a;返回字符串的长度。
let str hello world;
console.log(str.length); // 11indexOf#xff1a;返回字符串中指定字符或子串的位置表示文本数据。字符串常用方法有以下几种
length返回字符串的长度。
let str hello world;
console.log(str.length); // 11indexOf返回字符串中指定字符或子串的位置如果不存在则返回-1。
let str hello world;
console.log(str.indexOf(o)); // 4
console.log(str.indexOf(z)); // -1slice返回字符串中指定位置的子串。
let str hello world;
console.log(str.slice(0, 5)); // hello
console.log(str.slice(6)); // worldsubstring返回字符串中指定位置的子串与slice方法类似但不能接受负数参数。
let str hello world;
console.log(str.substring(0, 5)); // hello
console.log(str.substring(6)); // worldsubstr返回字符串中指定长度的子串第二个参数表示子串的长度。
let str hello world;
console.log(str.substr(0, 5)); // hello
console.log(str.substr(6)); // worldreplace替换字符串中的指定字符或子串。
let str hello world;
console.log(str.replace(world, JavaScript)); // hello JavaScripttoUpperCase将字符串转换为大写。
let str hello world;
console.log(str.toUpperCase()); // HELLO WORLDtoLowerCase将字符串转换为小写。
let str HELLO WORLD;
console.log(str.toLowerCase()); // hello worldtrim去除字符串两端的空格。
let str hello world ;
console.log(str.trim()); // hello world以上是JavaScript中字符串常用方法的详细讲解及举例。