截取字符

<template>
  <div>
    <button @click="test">点击</button>
  </div>
</template>
<script>
export default {
  data() {
    return {


    }
  },
  methods: {
    test() {
      const Data = []
      const arr = ['1s23','2s456','3s890','3s890','156s6','156s6','98s123']
      const DATAafter = []
      const DATAbefore = []
      // substring() 方法用于提取字符串中介于两个指定下标之间的字符。
      for( let a of arr) {
        // console.log(a)  //获取所有的字符串  打印结果为  1s23  2s456 3s890 3s890 156s6  156s6 98s123
        const Sindex = a.indexOf('s') // 获取 s 在每个字符串中的位置  打印结果为 1 1 1 1 3 3 2
        //获取到字符串中每个s所在的位置 下面进行截取。
        // 第一 获取s前面的字符串 
        const before = a.substring(0, Sindex) //  截取s前面的字符串  截取下表从零开始 结束在 Sindex位置
        // console.log(before) //  打印结果 1 2 3 3 156 156 98
        // 下面获取s后面的字
        const after = a.substring(Sindex+1) // 获取 Sindex+1后面的所有字符
        // console.log(after) // 打印结果 23 456 890 890 6 6 123
        // Data.push(before,after)
        DATAafter.push(after)
        DATAbefore.push(before)
      }
        console.log(DATAafter,DATAbefore)  //  打印的结果  ["1", "23", "2", "456", "3", "890", "3", "890", "156", "6", "156", "6", "98", "123"]
        const data = DATAbefore.concat(DATAafter)
        // 最后进行去重  最简单的es6中的set 去重
        const fdata = Array.from(new Set(Data))
        // console.log(fdata) // ["1", "23", "2", "456", "3", "890", "156", "6", "98", "123"]



        
    }
  }
}
</script>
原文地址:https://www.cnblogs.com/toughy/p/12011102.html