js || 字符串原型上方法

194 阅读2分钟

下标都是从0开始的

var str = "hello world"

image.png

concat() 加到字符串末尾

原来字符串内容不变

console.log(str.concat("a"));  //hello worlda
console.log(str);      	       //hello world

repeat() 复制字符串并拼接在一起

原来字符串内容不变

console.log(str.repeat(2))      //hello worldhello world  复制2次
console.log(str);     //hello world

padStart() 字符加到字符串开头

原来字符串内容不变,两个参数:第一总长度,第二要加的内容,超过长度显示不显示超过的

console.log(str.padStart(12,"a"));   //ahello world
console.log(str.padStart(12,"abc"));  //ahello world   超多

padEnd() 字符加到字符串末尾

原来字符串内容不变,两个参数:第一总长度,第二要加的内容,超过长度显示不显示超过的

console.log(str.padEnd(12,"a"))      //hello worlda
console.log(str.padEnd(12,"abc"))      //hello worlda   超多

trim() 去掉头尾空格

var str = " hello world "
console.log(str.trim());            
console.log(str.trimStart());       //hello world
console.log(str.trimEnd());         // hello world 
console.log(str.trimLeft());        //hello world 
console.log(str.trimRight());       // hello world

split() 字符串分割为字符串数组

不改变原始字符串,可选分隔符

console.log(str.split(""));      // ['h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd']
console.log(str.split(" "));     //['hello', 'world']

replace()、replaceAll() 替换字符

不改变原始字符串

console.log(str.replace("l","a"))      //healo world    修改第一个
console.log(str.replaceAll("l","a"))      //heaao worad    修改全部

toString() 进制转换

不改变原始字符串,参数是几进制

var str = 12
console.log(str.toString(2));   //1100
console.log(str.toString(8));   //14
console.log(str.toString(10));   //12
console.log(str.toString(16));   //c

at() 查找下标的字符

console.log(str.at(1));          //e

charAt() 查找下标的字符

console.log(str.charAt(1));      //e

charCodeAt() 查找指定位置字符的Unicode编码

console.log(str.charCodeAt(1));  //h   101  

indexOf() 查找字符出现的下标

console.log(str.indexOf("h"));   //0

includes() 字符串是否包含字符

console.log(str.includes("o"));  //true

lastIndexOf() 字符最后一次出现位置

console.log(str.lastIndexOf("l"));    //9

search() 查找字符出现的下标

找不到为-1

console.log(str.search("e"))      //1

截取

slice() 截取指定位置字符

不改变原始字符串

console.log(str.slice(1,3))      //el

substr() 截取指定位置字符

不改变原始字符串

console.log(str.substr(0,3));   //hel

substring() 截取指定位置字符

不改变原始字符串

console.log(str.substring(0,3));   //hel

判断

valueOf()

输出原始值还可以判断

console.log(str.valueOf());   //hello world
console.log(str.valueOf() === Array);   //FALSE

startsWith()、endsWith() 开头结尾

console.log(str.startsWith("hello"))   //true
console.log(str.endsWith("hello"))     //false

大小写转换

大写

var str = " hello world "
console.log(str.toUpperCase());        // HELLO WORLD 
console.log(str.toLocaleUpperCase());  // HELLO WORLD 

小写

var str = " hello world "
console.log(str.toLowerCase());        // hello world 
console.log(str.toLocaleLowerCase());  // hello world 

String HTML 包装方法

console.log(str.anchor());
console.log(str.big());                       //字体变大
console.log(str.blink());                     //闪动文本
console.log(str.bold());                     //字体变粗
console.log(str.fixed());                    //固定定位
console.log(str.fontcolor("red"));           //字体颜色为红色
console.log(str.fontsize("14px"));           //字体大小
console.log(str.italics());                  //字体变斜
console.log(str.link("http://www.xx.com"));   //链接
console.log(str.small());                     //字体变小
console.log(str.sub());                       //下标
console.log(str.sup());                       //上标