字符串常用方法

length 属性

字符串串长度

let str = "hello";
console.log(str.length)   // 5  字符串长度只能读取 不可修改
console.log(str[1])       // e  获取字符串中某一项 IE7及以下不识别
charAt(index)

查找返回指定位置的字符 (代替[ ]下标获取字符, 兼容IE8及其以下)

let str = "hello";
console.log(str.charAt(1));     // e  ===> 下标为1的字符
charcodeAt(index)

查找返回指定字符的unicode码

let str = "hello"
console.log(str.charCodeAt(2));   // 108    ===> 下标为5的字符的unicdoe码 
concat(str)

连接字符串,返回拼接后新字符串, 原字符串不改变

let str = "hello";     
let str1 = str.concat("aaa","bbb","ccc");
console.log(str1);    // helloaaabbbccc
console.log(str);     // hello    ===> 不改变原字符串
slice(start, end)

字符串截取,返回一个新的字符串, 原字符串不改变

// 截取子字符串 从start开始这一位开始截取到end这一位(算头不算尾)
// slice()  第二位参数传递负值 则代表索引值为倒数第几项,
// 两个参数数字大的不能在前,当第二位参数不为负值小于第一位参数时截取空字符 (有前后之分)
let str = "hello world";
let str1 = str.slice(1,5);
console.log(str1);  // ello 
let str2 = str.slice(1,-1);  //  ello worl
let str3 = str.slice(4,2);   // ''
var str4 = str.slice(-3, -1); 
console.log(str2); // ello worl
console.log(str3); // ''
console.log(str4); // rl
substring(start, end)

字符串截取,返回在字符串中指定两个下标之间的字符(算头不算尾), 原字符串不改变

// substring  传参为负值  会转换成0, 传参大于字符串长度就会转换为等于字符串长度
// 如果第一参数大于第二参数,则 substring 的执行效果就像两个参数调换了一样
// 第二个参数不写 默认截取所有的
// 如果任一参数小于 0 或为 NaN,则被当作 0
let str = "hello world";
let str1 = str.substring(1, 6); // 从下边1开始到下边6结束不包括下标6那一项
let str2 = str.substring(6, 1); // 同上行
let str3 = str.substring(1, -1);
console.log(str1);   // ello 
console.log(str2);   // ello 
console.log(str3);   // h 
substr(index,length)

字符串截取,返回指定位置开始的字符串长度的字符, 不改变原字符串

// 传递的参数第一个是开始索引下标,正值,且大于或等于字符串的长度,返回一个空字符串
// 第一参数传递为负值时,则把它作为从字符串末尾开始的一个字符索引
// 第二个是截取的长度,当长度为负值时则转换成0,返回一个空字符串,
// 如果忽略不写则 substr 提取字符,直到字符串末尾。
// 
let str = "hello world";
let str1 = str.substr(1,4);  // 从下标1开始往后数4个数
let str2 = str.substr(1,-3);
let str3 = str.substr(-4,2);  // 从后面下标为4开始往后2个数
console.log(str1);  // ello
console.log(str2);  // ''
console.log(str3);  // or
console.log(str);   // hello world
split(str)

将字符串变数组,从某一项切割前后变为字符串项, 返回一个数组 ,原字串符不改变

// 参数可以为字符串,也可以传递一个正则表达式,当不传递时本身的数组
let str = "hello"
console.log(str.split("l")); // ["he", "", "o"]
console.log(str.split("")); //  ["h", "e", "l", "l", "o"]
console.log(str.split(/e/)); // ["h", "llo"]
repeat(num)

返回指定重复次数的由元素组成的字符串对象 原字串符不改变

// 参数介于0和正无穷大之间的整数,表示在新构造的字符串中重复了多少遍原字符串
// 注意: 1. 重复次数不能为负数,, 2.重复次数必须小于 infinity,且长度不会大于最长的字符串
let str = "abc"
console.log(str.repeat(0)); // ''
console.log(str.repeat(1)); // abc
console.log(str.repeat(2)); // abcabc
console.log(str.repeat(3.5)); // abcabcabc   小数将会被自动转换成整数.
console.log(str.repeat(-1)); // 报错
console.log(str.repeat(1 / 0)); // 报错
replace(str)

被用来在正则表达式和字符串直接比较,然后用新的子串来替换被匹配的子, 原字符串不会改变

// 返回一个由替换值,替换一些或所有匹配的模式后的新字符串,
// 模式可以是一个字符串或者一个正则表达式,替换值可以是一个字符串或者一个每次匹配都要调用的回调函数
// 参数可以:
// 一个对象或者其字面量,该正则所匹配的内容会被第二参数的返回值贴换掉
// 一个被替换的字符串,视为一整个字符串,而不是一个正则表达式,仅第一个匹配项会被贴换
// 用于替换掉第一个参数在原字符串中的匹配部分的字符串。该字符串中可以内插一些特殊的变量名(可参考mdn)
// 一个用来创建新子字符串的函数,该函数的返回值将替换掉第一个参数匹配到的结果

// 使用正则匹配 (无全局匹配)
let str = "hello world the night before Hello world";   // 只会匹配第一个符合正则的字符串
let newstr1 = str.replace(/hello/,"hi");
console.log(newstr1);
// 使用正则匹配 (全局匹配等参数)
let newstr2= str.replace(/hello/ig,"hi")
console.log(newstr2);
// 使用普通字符串替换
let newstr3 = str.replace("hello","hi");
console.log(newstr3);  //  hi world the night before Hello world
// 使用特殊的变量名结合正则替换
let str = "John Smith";
let reg = /(\w+)\s(\w+)/;
let newstr4 = str.replace(reg,"$2,$1");  // 使用正则中子串进行将两个单词交换位置
console.log(newstr4); //Smith,John
// 使用函数创建新字符
function styleHyphenFormat(propertyName) {
  function upperToHyphenLower(match) {
    return '-' + match.toLowerCase();
  }
  return propertyName.replace(/[A-Z]/g, upperToHyphenLower);
}
console.log(styleHyphenFormat('borderTop')); // border-top
indexOf(str)

遍历字符串,查找包含第一个参数的字符串,如包含返回第一位的下标,停止查找,如没有返回 -1 值

第二个参数表示从此下标开始查找, 原字符串不改变

// 只有一个参数的时候
let str="hello"
console.log(str.indexOf("e"))   // 1
console.log(str.indexOf("a"));  // -1
console.log(str.indexOf("ll")); // 2
// 两个参数的时候:
	// 第一个参数是检索值 第二个参数从什么位置开始检索 没有返回-1
console.log(str.indexOf("o",1))  // 4
console.log(str);   // hello
lastIndexOf(str)

与indexOf()功能相同,区别在于从后往前找返回第一个索引,

从字符串对象中返回最后一次出现的索引值,如果没有找到则返回-1, 原字符串不改变

// 只有一个参数的时候 返回最后一次出现的位置
let str = "hello world";
console.log(str.lastIndexOf("o"));  // 7
console.log(str.lastIndexOf("ll"));  // 2
// 两个参数的时候:
     // 第一个参数是检索的值 第二个参数从什么位置开始检索向前找 没有返回 -1,当传递为负数时返回是 -1 
console.log(str.lastIndexOf("o", 8));  // 7
console.log(str); // hello world
String.formCharCode(num)

通过一串 Unicode 创建字符串

let str = "我喜欢你";
let s = "";
for (let i = 0; i < str.length; i++) {
     var ss = str.charCodeAt(i) + 520;
     console.log(ss);
     s += String.fromCharCode(ss);
 }
 console.log(s);  // 搙垤洪全
toUpperCase()

将字符串转换成小写的字符 不接受传递参数 原字符串不改变

let str = "Hello World";
console.log(str.toUpperCase());   // HELLO WORLD
console.log(str);  // Hello World
toLowerCase()

将字符串转换成大写的字符 不接受传递参数 原字符串不改变

let str = "Hello World";
console.log(str.toLowerCase());   // hello world
console.log(str);  // Hello World
includes(str)

判断一个字符串里是否包含指定字符串 结果返回布尔值 es6中的 原字符串不改变

// 第一个参数是要搜索的字符串, 第二参数可选,表示从哪个索引位置开始搜子字符串,默认值为0
// 如果当前字符串包含搜索的字符串,返回true; 否则返回false
let str = "hello world";
let result1 = str.includes("o");  
let rusult2 = str.includes("cl");
let rusult3 = str.includes("e",3);
console.log(result1);  // true
console.log(result2);  // true
console.log(result3);  // false
endsWith(str)

判断一个字符串的是否以给定字符串结尾,结果返回布尔值 原字符串不改变

// 第一个参数搜索的子字符串, 第二参数可选,作为字符串长度,默认是str.length
// 如果传入的子字符串在搜索字符串的末尾则返回true;否则将返回 false
let str = "hello world";
let result1 = str.endsWith("world");   // 检测尾部
let result2 = str.endsWith("wo",8);    // 检测指定位置是否以指定字符结尾
console.log(result1);   // true
console.log(result2);   // true
console.log(str);   // hello world
startsWith(str)

判断当前字符串是否以另外一个给定的子字符串开头,并根据判断结果布尔值, 原字符串不改变

// 第一参数 要搜索的子字符串, 第二参数可选,表示开始搜索位置,默认值为0,也就是真正的字符串开头处
// 如果在字符串的开头找到了给定的字符则返回true;否则, 返回false
let str = "hello world";
let result1 = str.startsWith("hello");   // 检测头部
let result2 = str.startsWith("lo",3);    // 检测指定位置是否以指定字符开头
console.log(result1);   // true
console.log(result2);   // true
console.log(str);   // hello world
match(reg)

使用正则表达式与字符串相比,较查找字符串 原字符串不改变

// 参数为  一个正则表达式对象,
// 如果传入一个非正则表达式对象,则会隐式地使用 new RegExp() 
// 返回值,如果使用g标志,则将返回与完整正则表达式匹配的所有结果,但不会返回捕获组
// 如果未使用g标志,则仅返回第一个完整匹配及其相关的捕获组(Array)属性有 groups: 一个捕获组数组,
// index: 匹配的结果的开始位置,,  input: 搜索的字符串,, 如果未找到匹配则为null;

// 传递参数为正则为全局匹配时
let str = "ADKDRLDFLTKCdfklsfg";
let reg = /[A-D]/ig;
let result1 = str.match(reg); 
console.log(result1);  // ["A", "D", "D", "D", "C", "d"]

// 传递参数为正则不为全局匹配时
let str = "fdsgSDFDGdfsaa";
let reg = /[a-d]/;
let result2 = str.match(reg);
console.log(result2);
// ["d", index: 1, input: "fdsgSDFDGdfsaa", groups: undefined]

// 不传递参数
let str = "sdfsfsSFDSG";
let result3 = str.match();
console.log(result3);
// ["", index: 0, input: "sdfsfsSFDSG", groups: undefined]

// 一个非正则表达式对象作为参数 (会隐式转换为正则)
let str3 = "The contract was declared null and void.";
let result4 = str3.match(null);
console.log(result4);
// ["null", index: 26, input: "The contract was declared null and void.", groups: undefined]
padEnd(num,str)

在当前字符串尾部填充指定的字符串, 直到达到指定的长度。 返回一个新的字符串

// 第一个参数,当前字符串需要填充到的目标长度。如果这个数值小于当前字符串的长度,则返回当前字符串本身
// 第二参数可选,为填充字符串,如果字符串太长,使填充后的字符串长度超过了目标长度,则只保留最左侧的部      分,其他部分会被截断
// 当只有第一个参数时,要达到指定长度则使用 " " 空字符串来填重到指定长度
// 返回值,在原字符串末尾填充指定的填充字符串直到目标长度所形成的新字符串
let str = "abc";
console.log(str.padEnd(10)); // "abc       "
console.log(str.padEnd(10, "ab")); // abcabababa
console.log(str.padEnd(6, "hello world")); // abchel
console.log(str.padEnd(2)); // abc
padStart(num,str)

在当前字符串头部填充指定的字符串, 直到达到指定的长度。 返回一个新的字符串

// 使用规则跟padEnd() 一样,区别在于padEnd()方法在字符串尾部添加,而padStart()方法在字符串头部添加
let str = "abc";
console.log(str.padStart(10)); // "       abc"
console.log(str.padStart(10, "ab")); // abababaabc
console.log(str.padStart(6, "hello world")); // helabc
console.log(str.padStart(2)); // abc
toString()

返回用字符串表示的特定对象,重写 Object.prototype.toString 方法

// 返回指定对象的字符串形式,  将包装对象转化为字符串形式
// 可以传入参数,传入的参数可以将数字转换为对应参数的进制字符串 (进制范围 2 - 36 之间)
let str = new String("hello world");
console.log(str);    // {"hello world"}
console.log(str.toString());   // hello world
search(reg)

对正则表达式和指定字符串进行匹配搜索,是否存在某个模式,返回第一个出现的匹配项的下标

// 参数为正则表达式对象,如果传入一个非正则表达式对象时,则会使用 new RegExp() 隐式地将其转换为正则表达式,
// 如果匹配成功,则 search() 返回正则表达式在字符串中首次匹配项的索引;否则,返回 -1
// 这个查询是否搜索匹配跟 indexOf() 功能相似,区别于 search() 是传入正则,查找符合正则返回下标
let str = "hello World";
let reg = /[A-Z]/g;
let reg2 = /[.sd]/;
console.log(str.search(reg));    // 6     匹配成功返回成功第一个下标索引
console.log(str.search(reg2))     // -1    匹配不到返回-1
trim()

从字符串的开始和结尾去除空格,返回一个从两头去掉空白字符的字符串,并不影响原字符串本身

// 使用此方法是不用传递参数的
let str = "    hello world    ";
console.log(str.trim());  // hello world
console.log(str);  //  "    hello world    "
trimeStart() == trimLeft()

从字符串的左侧去除空格, 不改变原字符串

// 移除原字符串左端的连续空白符并返回一个新字符串,并不会直接修改原字符串本身
// 不需要传递参数
let str = "   foo  ";
console.log(str.length);  // 8
console.log(str.trimStart());  // "foo  "
console.log(str.trimLeft());  // "foo  "
console.log(str)  // "   foo  "
trimeEnd() == trimRight()

从字符串的右侧去除空格

// 使用跟trimeStart() 一样
let str = "   foo  ";
console.log(str.length);  // 8
console.log(str.trimEnd());      //  "   foo"
console.log(str.trimRight());    //  "   foo"
console.log(str)                 //  "   foo  "	

总结:

​ 字符串的操作方法一般是不改变原先字符串的,都是生成新字符串!!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值