高阶函数处理字符串方法

发布时间 2023-06-01 14:43:22作者: 会飞的小白

1、concat()用于将一个或多个字符串拼接成一个新字符串。来看下面的例子:

let stringValue = "hello "; 
let result = stringValue.concat("world"); 
//可接收任意多个参数
let res = stringValue.concat("world","!!");
 
console.log(result);      // "hello world" 
console.log(stringValue); // "hello"
console.log(res); // "hello world !!"

2、3个从字符串中提取子字符串的方法slice('ks','js') 、 substr() 和 substring()

都接收两个参数一开始位置二结束位置
slice() 和 substring()第二个参数是提取结束的位置(即该位置之前的字符会被提取出来)
substr() 第二个参数表示返回的字符数

let stringValue = "hello world"; 
console.log(stringValue.slice(3));       // "lo world" 
console.log(stringValue.substring(3));   // "lo world" 
console.log(stringValue.substr(3));      // "lo world" 
console.log(stringValue.slice(3, 7));    // "lo w" 
console.log(stringValue.substring(3,7)); // "lo w" 
console.log(stringValue.substr(3, 7));   // "lo worl"

3、字符串位置方法indexOf() 和lastIndexOf() 没有找到返回 -1,有就返回最后检索到的位置

lastIndexOf() 在面对字符串是中文的时候会失效

let stringValue = "hello world"; 
console.log(stringValue.indexOf("o", 6));     // 7 
console.log(stringValue.lastIndexOf("o", 6)); // 4
//indexOf() 返回 7 ,因为它从位置6(字符 "w" )开始向后搜索字符串,在位置7找到了 "o" 。而 lastIndexOf() 返回 4 ,因为它从位置6开始反向搜索至字符串开头,因此找到了 "hello" 中的 "o" 


let stringValue = "Lorem ipsum dolor sit amet, 
consectetur adipisicing elit"; 
let positions = new Array(); 
let pos = stringValue.indexOf("e"); 
 
while(pos > -1) { 
  positions.push(pos); 
  //检索位置为上一个再加1
  pos = stringValue.indexOf("e", pos + 1); 
} 
 
console.log(positions); // [3,24,32,35,52]

4、字符串包含方法

startsWith() 、 endsWith() 和 includes()
这些方法都会从字符串中搜索传入的字符串,并返回一个表示是否包含的布尔值。

区别:startsWith() 检查开始于索引0的匹配项
endsWith() 检查开始于索引 (string.length - substring.length) 的匹配项
includes() 检查整个字符串

let message = "foobarbaz"; 
 
console.log(message.startsWith("foo"));  // true 
console.log(message.startsWith("bar"));  // false 
 
console.log(message.endsWith("baz"));    // true 
console.log(message.endsWith("bar"));    // false 
 
console.log(message.includes("bar"));    // true 
console.log(message.includes("qux"));    // false

startsWith() 和 includes()方法接收可选的第二个参数,表示
开始搜索的位置。如果传入第二个参数,则意味着这两个方法会从指定
位置向着字符串末尾搜索,忽略该位置之前的所有字符。下面是一个例
子:

let message = "foobarbaz"; 
 
console.log(message.startsWith("foo"));     // true 
console.log(message.startsWith("foo", 1));  // false 
 
console.log(message.includes("bar"));       // true 
console.log(message.includes("bar", 4));    // false

endsWith() 方法接收可选的第二个参数,表示应该当作字符串末尾
的位置。如果不提供这个参数,那么默认就是字符串长度。如果提供这
个参数,那么就好像字符串只有那么多字符一样:

let message = "foobarbaz"; 
 
console.log(message.endsWith("bar"));     // false 
console.log(message.endsWith("bar", 6));  // true

5、trim() 方法:删除字符串前后所有空格
另外, trimeLeft() 和 trimRight() 方法分别用于从字符串开始和末尾清理空格符。

let stringValue = " hello world "
let trimStringValue = stringValue.trim();
console.log(stringValue) // " hello world "
console.log(trimStringValue) // "hello world"

6、Math:Math 对象作为保存数学公式、信息和计算的地方。 Math 对象提供了一些辅助计算的属性和方法

属性 说明
Math.E 自然对数的基数e的值
Math.LN10 10为底的自然对数
Math.LN2 2为底的自然对数
Math.LOG2E 以2为底e的对数
Math.LOG10E 以10为底e的对数
Math.PI π的值
Math.SQRT1_2 1/2的平方根
Math.SQRT2 2的平方根

7、解决遍历对象时,把原型上的属性遍历出来了咋办?

使用hasOwnProperty判断
hasOwnProperty表示是否有自己的属性。这个方法会查找一个对象是否有某个属性,但是不会去查找它的原型链。

function Person(name) {
  this.name = name
}
Person.prototype.age = 23
const person = new Person('Sunshine_lin')
for (const key in person) { console.log(key) } // name age
// 使用 hasOwnProperty
for (const key in person) {
  person.hasOwnProperty(key) && console.log(key)
} // name

8、如何实现数组去重?

// 使用 Map 去重
function quchong1(arr) {
  const newArr = []
  arr.reduce((pre, next) => {
    if (!pre.get(next)) {
      pre.set(next, 1)
      newArr.push(next)
    }
    return pre
  }, new Map())
  return newArr
}

// 使用 Set 去重
function quchong (arr) {
    return [...new Set(arr)]
}

9、Set与Array的区别是什么?

Set使用has判断有无元素,数组使用索引
Set添加元素使用方法add,数组用push、unshift
Set长度为size,数组为length
Set会自动把同样的基础数据类型去重,数组不能
Set删除元素用delete,数组用splice、pop、shift
Set可以使用clear清空,数组需要重新赋值[]
数组可以传入new Set(array),实现数组转Set
Set可以使用keys、value方法,转数组
Set自带forEach方法进行遍历