目录
1.str.toLowerCase() 转换小写, 返回新的字符串
2.str.toUpperCase() 转换大写 返回新的字符串
3.str.charAt(index) 根据索引获取字符串中的某一个字符
4.str.charCodeAt(index)根据索引获取字符串中的某一个字符的ASCII编码
5.str.startsWith()判断一个字符串是否以某个字符或字符串开头返回布尔值
6.str.endsWith()判断一个字符串是否以某个字符或字符串结尾返回布尔值
7.str.includes(searchvalue,start)判断一个字符串中是否包含某个字符串,返回布尔值
9.str.replace(oldstr,newstr) 替换字符串中的某一部分,返回新的字符串
10.str.replaceAll(oldstr,newstr) 替换所有给定的字符串,返回新的字符串
11.str.split(separator,howmany)用于把一个字符串通过以符号、字符为标准分割成字符串数组
12.substr(indexStart,lenght) 截取字符串
13.substring(indexStart, indexEnd) 截取 含头不含尾
14.slice(indexStart, indexEnd) 截取
15.indexOf(searchElement, fromIndex) 查询指定字符在指定位置后面第一次出现的索引
一、转换
1.str.toLowerCase() 转换小写, 返回新的字符串
let a = 'ALPHABET';
var str = a.toLowerCase();
console.log(str);//alphabet
console.log(a);//ALPHABET
2.str.toUpperCase() 转换大写 返回新的字符串
let a = 'alphabet';
var str = a.toUpperCase();
console.log(str);//ALPHABET
console.log(a);//alphabet
二、索引
3.str.charAt(index) 根据索引获取字符串中的某一个字符
下标越界时得到空字符串
function demoCharAt() {
var str = "Brave new world";
console.log(str.charAt(0) + "'");//B
console.log(str.charAt(1) + "'");//r
console.log(str.charAt(2) + "'");//a
console.log(str.charAt(3) + "'");//v
console.log(str.charAt(4) + "'");//e
console.log(str[0] + "'");//B
console.log(str[1] + "'");//r
console.log(str[2] + "'");//a
console.log(str[3] + "'");//v
console.log(str[4] + "'");//e
// 下标越界时得到空字符串
console.log(str.charAt(999) + "'");//
}
demoCharAt()
4.str.charCodeAt(index)根据索引获取字符串中的某一个字符的ASCII编码
function demoCharCodeAt() {
var str = "aa"
console.log("妹的码点(在字符集中的序号)是", str.charCodeAt(0));//20320
}
demoCharCodeAt()
三、判断
5.str.startsWith()判断一个字符串是否以某个字符或字符串开头返回布尔值
let str1 = "Hello World";
console.log(str1.startsWith("Hello")); //true
6.str.endsWith()判断一个字符串是否以某个字符或字符串结尾返回布尔值
let str1 = "Hello World";
console.log(str1.endsWith("ld")); //true
7.str.includes(searchvalue,start)判断一个字符串中是否包含某个字符串,返回布尔值
includes(要查找的字符串(必填),设置从那个位置开始查找,默认为 0(选填))
let str1 = "Hello World";
console.log(str1.includes("Hello")); //true
let str2 = "hello,你好,hello";
console.log(str2.includes("hello", 10));//false
四、去除
8.str.trim() 去除字符串两端的空格
var str3 = " 你 好 ";
console.log(str3);
console.log(str3.trim());//"你好"
五、替换
9.str.replace(oldstr,newstr) 替换字符串中的某一部分,返回新的字符串
replace(旧字符串,新字符串)
let str1 = "Hello World";
console.log(str1.replace("World", "世界"));//"Hello 世界"
10.str.replaceAll(oldstr,newstr) 替换所有给定的字符串,返回新的字符串
/* 自己编辑一个敏感词数组 将指定文章中的所有敏感词都替换为等量的* */
function fn01x() {
var dirtyWords = ["你妹", "你妹啊", "尼玛", "我草", "shit", "fuck"]
var passage = "shit!你妹啊,姐尼玛三个月没来大姨妈了,shit!急死老娘了,真是坑爹啊!"
console.log(passage);
for (var word of dirtyWords) {
passage = passage.replaceAll(word, function (str) {
var newstr = ""
for (var i = 0; i < str.length; i++) {
newstr += "*"
}
return newstr
})
console.log(word, passage);
}
// console.log(cleanPassage);
}
六、截取
11.str.split(separator,howmany)用于把一个字符串通过以符号、字符为标准分割成字符串数组
separator参数:必需填。字符串或正则表达式,从该参数指定的地方分割 stringObject
howmany参数:可选。该参数可指定返回的数组的最大长度。如果设置了该参数,返回的子串不会多于这个参数指定的数组。如果没有设置该参数,整个字符串都会被分割,不考虑它的长度
<script type="text/javascript" charset="utf-8">
var str = "I lover you too";
var aa = str.split(" ");
console.log(aa);// ['I', 'lover', 'you', 'too']
var bb = str.split("");
console.log(bb);
//返回["I", " ", "l", "o", "v", "e", "r", " ", "y", "o", "u", " ", "t", "o", "o"]
//不传任何切割标志时,默认切割每一个字符
var cc = str.split(" ",2); // 先转数组后切割
console.log(cc);
//返回["I", "lover"],以空格切割,返回前两个数组元素
</script>
12.substr(indexStart,lenght) 截取字符串
var str = "aabhellohcchdd";
console.log(str.substr(3, 5)); //hello
13.substring(indexStart, indexEnd) 截取 含头不含尾
substring(indexStart, indexEnd) 截取范围含头不含尾(indexStart,indexEnd-1)
function demoSubstring() {
var str = "Mozilla";
// 截取范围[0,3) 含头不含尾/左闭右开
console.log(str.substring(0, 3)); //Moz
}
如果indexStart 大于indexEnd,哪个小哪个为起始位置
14.slice(indexStart, indexEnd) 截取
当slice只存在一个值时,从头截取到尾
function demoSlice() {
const str = "The quick brown fox jumps over the lazy dog.";
// 从31位截取到末尾
console.log(str.slice(31));
// expected output: "the lazy dog."
// 截取范围[4,18]
console.log(str.slice(4, 19));
// expected output: "quick brown fox"
// 从倒数第4位一直截取到末尾
console.log(str.slice(-4));
// expected output: "dog."
// 从倒数第9位一直截取到倒数第6位、
// The quick brown fox jumps over the lazy dog.
console.log(str.slice(-9, -5));
// expected output: "lazy"
}
七、查询
15.indexOf(searchElement, fromIndex) 查询指定字符在指定位置后面第一次出现的索引
indexOf(查询的值(必填), 从哪个下标开始(选填,默认为0))
var str = "aabhellohcchdd";
console.log(str.indexOf("h")); //3 查询h在str5中第一次出现的索引号
var backIndex = str.indexOf("h");//返回的是第一次出现位置的下标
console.log(str.indexOf("h", backIndex+1));
//backIndex+1 将前一次出现的位置下标+1使其检索不到,延到下一次值得出现
//8 从指定位置查询 h 在 str中第一次出现的索引号。
// lastIndexOf()(反向查找)查询指定字符在字符串中最后一次出现的位置