一:字符方法
charAt():返回指定下标的字符
charCodeAt():返回指定下标字符对应的编码
let str = 'hello world';
console.log(str.charAt(1)); // e
console.log(str.charCodeAt(1)); // 101
二:字符串操作方法
1:字符串拼接 concat: 连接两个或多个字符串
let str = 'hello';
console.log(str.concat(' world')); // hello world
console.log(str.concat(' a', ' b')); // hello a b
str += ' world'; // hello world
2:字符串截取
slice(s, e): s为起始位置,e为结束位置
substring(s/e, e/s): s大于e时,e为s,否则位置不变;e最小值为0,负值默认为0
substr(s, len): len为截取长度
let str = 'hello world';
/* 一个参数 */
// 从指定位置开始截取,结果相同
console.log(str.slice(3)); // lo world
console.log(str.substring(3)); // lo world
console.log(str.substr(3)); // lo world
/* 两个参数 */
// 从指定位置向后截取,slice第二个参数为截取指定长度
console.log(str.slice(3, 7)); // lo w
console.log(str.substring(3, 7)); // lo w
console.log(str.substr(3, 7)); // lo worl
/* 负值 */
// 第二个参数为负值,slice第二个参数 = str长度+负值
// substring第二个参数为0,取0开始到3结束的字符串
// substr 返回空字符串
console.log(str.slice(3, -2)); // lo wor
console.log(str.substring(3, -2)); // hel
console.log(str.substr(3, -2)); // ""
3:获取字符串出现的位置
indexOf: 从前向后搜索子字符串在字符串中出现的位置
lastIndexOf: 与indexOf顺序相反(从后向前)
let str = 'hello world';
console.log(str.indexOf("o")); // 4
console.log(str.lastIndexOf("o")); // 7
console.log(str.indexOf("o", 6)); // 7
console.log(str.lastIndexOf('o', 6)); // 4
console.log(str.indexOf('a')); // -1 搜索内容不存在,返回-1
4:删除空格
trim: 删除字符串前后空格
let str = ' hello world ';
console.log('(' + str.trim() + ')'); // hello world
console.log('(' + str + ')'); // ( hello world )
5:字符串大小写转换
toLowerCase: 转换为小写
toUpperCase: 转换为大写
let str = 'Hello World';
console.log(str.toLowerCase()); // hello world
console.log(str.toUpperCase()); // HELLO WORLD
6:字符串模板匹配
match: 参数为字符串或RegExp对象指定的正则表达式
search: 参数为字符串或RegExp对象指定的正则表达式
let str = 'cat,bat,sat,fat';
let pattern = /.at/;
let matches = str.match(pattern);
console.log(matches.iondex); // 0
console.log(matches[0]); // cat
console.log(pattern.lastIndex); // 0
let pos = str.search(/at/);
console.log(pos); // 1
7:字符串替换
replace(str, restr): str为匹配项,restr为替换内容
let str = 'cat,bat,sat,fat';
let res = str.replace("at", "all");
console.log(res); // call,bat,sat,fat
let res2 = str.replace(/at/g, "all"); // g匹配全局匹配项
console.log(res2); // call,ball,sall,fall
8:字符串分割
split(",", num): 参数1为匹配符号,num为分割数量
let str = 'red,blue,green';
console.log(str.split(",")); // ["red", "blue", "green"]
console.log(str.split("-")); // ["red,blue,green"]
console.log(str.split(",", 2)); // ["red", "blue"]
9:字符串比较
localeCompare 从前到后使用字母表比较两个字符串
/*
a.localeCompare(b)
① a 排在前,返回负数;
② a排在后,返回正数;
③ a===b 返回0;
④ 区分大小写,A排在z后
*/
let str = "yellow";
console.log(str.localeCompare("bank")); // 1
console.log(str.localeCompare("yellow")); // 0
console.log(str.localeCompare("yelloW")); // -1
console.log(str.localeCompare("zoo")); // -1
10:编码转换为字符串
fromCharCode: 接收一或多个字符编码,将其转为字符串
fromCharCode是String构造函数的一个静态方法
console.log(String.fromCharCode(104, 101, 108, 108, 111)); // hello
三:日常组合用法
1:获取匹配字符串所在的各个位置
let str = 'asdflagldagsea';
let posArr = [];
let pos = str.indexOf("a");
// 此处使用if只返回第一次查询到的结果
while (pos > -1) {
posArr.push(pos);
pos = str.indexOf("a", pos + 1);
}
console.log(posArr); // [0, 5, 9, 13]
2:字符串去重
let str = "aabbcdaanvdfaa";//这里字符串没有可以分隔的字符,所以需要使用空字符串作为分隔符
function unique(msg) {
let res = [];
let arr = msg.split("");
console.log(arr); // ['a', 'a', 'b', 'b', 'c', 'd', 'a', 'a', 'n', 'v', 'd', 'f', 'a', 'a']
for (let i = 0; i < arr.length; i++) {
if (res.indexOf(arr[i]) == -1) {
res.push(arr[i]);
}
}
return res.join("");
}
console.log(unique(str)); // abcdnvf
3:判断字符串中字符出现的次数
/*
思路
1.先实现字符串去重
2.然后对去重后的数组用for循环操作,分别与原始数组中各个值进行比较,如果相等则count++,循环结束将count保存在sum数组中,然后将count重置为0
3.这样一来去重后的数组中的元素在原数组中出现的次数与sum数组中的元素是一一对应的
*/
let str = "aacccbbeeeddd";
let sum = [];
let res = [];
let count = 0;
let arr = str.split("");
for (let i = 0; i < arr.length; i++) {
if (res.indexOf(arr[i]) == -1) {
res.push(arr[i]);
}
}
for (let i = 0; i < res.length; i++) {
for (let j = 0; j < arr.length; j++) {
if (arr[j] == res[i]) {
count++;
}
}
sum.push(count);
count = 0;
}
console.log(res); // ["a", "c", "b", "e", "d"]
for (let i = 0; i < res.length; i++) {
let str = (sum[i] % 2 == 0) ? "偶数" : "奇数";
console.log(res[i] + "出现了" + sum[i] + "次");
console.log(res[i] + "出现了" + str + "次");
}
题外话:阿里面试字符串操作
推测问题:颠倒字符串并去除tao
/*
* split 分割字符串
* reverse 颠倒字符串
* join 数组操作方法, 将数组中所有元素转换为一个字符串
* replace 截取oat,替换为空
*/
var str = "www.taobao.com";
var res = str.split("") // ['w', 'w', 'w', '.', 't', 'a', 'o', 'b', 'a', 'o', '.', 'c', 'o', 'm']
.reverse() // ['m', 'o', 'c', '.', 'o', 'a', 'b', 'o', 'a', 't', '.', 'w', 'w', 'w']
.join("") // m,o,c,.,o,a,b,o,a,t,.,w,w,w
.replace('oat',''); // moc.oab.www
console.log(res);//moc.oab.www
全部代码
// 字符方法
// charAt() 返回指定下标的字符
// charCodeAt() 返回指定下标字符对应的编码
let str = 'hello world';
console.log(str.charAt(1)); // e
console.log(str.charCodeAt(1)); // 101
// 字符串操作方法
// 字符串拼接
// concat: 连接两个或多个字符串
let str = 'hello';
console.log(str.concat(' world')); // hello world
console.log(str.concat(' a', ' b')); // hello a b
str += ' world'; // hello world
// 字符串截取
// slice(s, e): s为起始位置,e为结束位置
// substring(s/e, e/s): s大于e时,e为s,否则位置不变;e最小值为0,负值默认为0
// substr(s, len): len为截取长度
let str = 'hello world';
/* 一个参数 */
// 从指定位置开始截取,结果相同
console.log(str.slice(3)); // lo world
console.log(str.substring(3)); // lo world
console.log(str.substr(3)); // lo world
/* 两个参数 */
// 从指定位置向后截取,slice第二个参数为截取指定长度
console.log(str.slice(3, 7)); // lo w
console.log(str.substring(3, 7)); // lo w
console.log(str.substr(3, 7)); // lo worl
/* 负值 */
// 第二个参数为负值,slice第二个参数 = str长度+负值
// substring第二个参数为0,取0开始到3结束的字符串
// substr 返回空字符串
console.log(str.slice(3, -2)); // lo wor
console.log(str.substring(3, -2)); // hel
console.log(str.substr(3, -2)); // ""
// 获取字符串出现的位置
// indexOf: 从前向后搜索子字符串在字符串中出现的位置
// lastIndexOf: 与indexOf顺序相反(从后向前)
let str = 'hello world';
console.log(str.indexOf("o")); // 4
console.log(str.lastIndexOf("o")); // 7
console.log(str.indexOf("o", 6)); // 7
console.log(str.lastIndexOf('o', 6)); // 4
console.log(str.indexOf('a')); // -1 搜索内容不存在,返回-1
// 删除空格
// trim: 删除字符串前后空格
let str = ' hello world ';
console.log('(' + str.trim() + ')'); // hello world
console.log('(' + str + ')'); // ( hello world )
// 字符串大小写转换
// toLowerCase: 转换为小写
// toUpperCase: 转换为大写
let str = 'Hello World';
console.log(str.toLowerCase()); // hello world
console.log(str.toUpperCase()); // HELLO WORLD
// 字符串模板匹配
// match: 参数为字符串或RegExp对象指定的正则表达式
// search: 参数为字符串或RegExp对象指定的正则表达式
let str = 'cat,bat,sat,fat';
let pattern = /.at/;
let matches = str.match(pattern);
console.log(matches.iondex); // 0
console.log(matches[0]); // cat
console.log(pattern.lastIndex); // 0
let pos = str.search(/at/);
console.log(pos); // 1
// 字符串替换
// replace(str, restr): str为匹配项,restr为替换内容
let str = 'cat,bat,sat,fat';
let res = str.replace("at", "all");
console.log(res); // call,bat,sat,fat
let res2 = str.replace(/at/g, "all"); // g匹配全局匹配项
console.log(res2); // call,ball,sall,fall
// 字符串分割
// split(",", num): 参数1为匹配符号,num为分割数量
let str = 'red,blue,green';
console.log(str.split(",")); // ["red", "blue", "green"]
console.log(str.split("-")); // ["red,blue,green"]
console.log(str.split(",", 2)); // ["red", "blue"]
// 字符串比较
// localeCompare 从前到后使用字母表比较两个字符串
/*
a.localeCompare(b)
① a 排在前,返回负数;
② a排在后,返回正数;
③ a===b 返回0;
④ 区分大小写,A排在z后
*/
let str = "yellow";
console.log(str.localeCompare("bank")); // 1
console.log(str.localeCompare("yellow")); // 0
console.log(str.localeCompare("yelloW")); // -1
console.log(str.localeCompare("zoo")); // -1
// 编码转换为字符串
// fromCharCode: 接收一或多个字符编码,将其转为字符串
// fromCharCode是String构造函数的一个静态方法
console.log(String.fromCharCode(104, 101, 108, 108, 111)); // hello
// 获取匹配字符串所在的各个位置
let str = 'asdflagldagsea';
let posArr = [];
let pos = str.indexOf("a");
// 此处使用if只返回第一次查询到的结果
while (pos > -1) {
posArr.push(pos);
pos = str.indexOf("a", pos + 1);
}
console.log(posArr); // [0, 5, 9, 13]
// 字符串去重
let str = "aabbcdaanvdfaa";//这里字符串没有可以分隔的字符,所以需要使用空字符串作为分隔符
function unique(msg) {
let res = [];
let arr = msg.split("");
console.log(arr); // ['a', 'a', 'b', 'b', 'c', 'd', 'a', 'a', 'n', 'v', 'd', 'f', 'a', 'a']
for (let i = 0; i < arr.length; i++) {
if (res.indexOf(arr[i]) == -1) {
res.push(arr[i]);
}
}
return res.join("");
}
console.log(unique(str)); // abcdnvf
// 判断字符串中字符出现的次数
/*
思路
1.先实现字符串去重
2.然后对去重后的数组用for循环操作,分别与原始数组中各个值进行比较,如果相等则count++,循环结束将count保存在sum数组中,然后将count重置为0
3.这样一来去重后的数组中的元素在原数组中出现的次数与sum数组中的元素是一一对应的
*/
let str = "aacccbbeeeddd";
let sum = [];
let res = [];
let count = 0;
let arr = str.split("");
for (let i = 0; i < arr.length; i++) {
if (res.indexOf(arr[i]) == -1) {
res.push(arr[i]);
}
}
for (let i = 0; i < res.length; i++) {
for (let j = 0; j < arr.length; j++) {
if (arr[j] == res[i]) {
count++;
}
}
sum.push(count);
count = 0;
}
console.log(res); // ["a", "c", "b", "e", "d"]
for (let i = 0; i < res.length; i++) {
let str = (sum[i] % 2 == 0) ? "偶数" : "奇数";
console.log(res[i] + "出现了" + sum[i] + "次");
console.log(res[i] + "出现了" + str + "次");
}
// 阿里面试字符串操作
/*
* 颠倒字符串并去除tao
* split 分割字符串
* reverse 颠倒字符串
* join 数组操作方法, 将数组中所有元素转换为一个字符串
* replace 截取oat,替换为空
*/
var str = "www.taobao.com";
var res = str.split("") // ['w', 'w', 'w', '.', 't', 'a', 'o', 'b', 'a', 'o', '.', 'c', 'o', 'm']
.reverse() // ['m', 'o', 'c', '.', 'o', 'a', 'b', 'o', 'a', 't', '.', 'w', 'w', 'w']
.join("") // m,o,c,.,o,a,b,o,a,t,.,w,w,w
.replace('oat',''); // moc.oab.www
console.log(res);//moc.oab.www
摘自:https://www.jb51.net/article/97915.htm