最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

js如何在字符串中查找某個字符的位置

 更新時間:2023年11月11日 10:19:03   作者:于扶搖  
這篇文章主要給大家介紹了關(guān)于js如何在字符串中查找某個字符的位置的相關(guān)資料,在JavaScript中我們經(jīng)常需要對字符串進行各種操作,包括查找包含特定字符的字符串,需要的朋友可以參考下

可以使用 JavaScript 中的字符串方法來查找某個字符在字符串中的位置。以下是一些示例:

在字符串中查找某個字符的位置:

const str = "Hello, world!";  
const search = "o";  
const index = str.indexOf(search);  
  
if (index !== -1) {  
  console.log(`The character '${search}' is found at index '${index}'.`);  
} else {  
  console.log(`The character '${search}' is not found in the string.`);  
}

在字符串中查找某個字符的位置,并打印出該字符的值:

const str = "Hello, world!";  
const search = "o";  
const index = str.indexOf(search);  
  
if (index !== -1) {  
  console.log(`The character '${search}' is found at index '${index}'.`);  
  console.log(str.charAt(index));  
} else {  
  console.log(`The character '${search}' is not found in the string.`);  
}

在字符串中查找某個字符的位置,并將該字符替換為另一個字符:

const str = "Hello, world!";  
const search = "o";  
const newStr = str.replace(search, "x");  
  
console.log(`The character '${search}' is found at index '${index}'.`);  
console.log(newStr);

在這些示例中,我們使用了 indexOf() 方法來查找字符串中的某個字符,并使用 charAt() 方法來獲取該字符在字符串中的位置。如果找到了該字符,我們使用 charAt() 方法來獲取該字符的值,并將其打印出來。如果沒有找到該字符,我們使用 -1 作為返回值來表示該字符不在字符串中。

附:js 在字符串中快速查找字符串并指出對應(yīng)的起始位置和結(jié)束位置

let a = 'dafdsafasdfadgdafasfsagadfasfsafsadfsda';
let str = 'da';
let b = a.indexOf(str);
let i = 0;
let locationList = [];
let locationObj = {};
 
while(a.indexOf(str,i) !== -1){
locationObj = {};
locationObj.startNum = a.indexOf(str,i);
locationObj.endNum = a.indexOf(str,i) + str.length - 1
locationList.push(locationObj);
console.log("開始",i);
console.log("起始位置",a.indexOf(str,i));
console.log("結(jié)束位置",a.indexOf(str,i) + str.length - 1);
console.log("三種情況:1、選擇位置在首位,2、選擇位置在中間,3、選擇位置在末尾");
  i = a.indexOf(str,i) + str.length;
}
let d = a.split(str);
for(let i = d.length - 1;i >=0 ; i--){
    if(i !== 0){
      d.splice(i , 0 ,str);
    }
}
if(d[0] === '') d.splice(0,1);
if(d[d.length - 1] === '') d.splice(d.length - 1,1);
console.log("c===================>", d);
 
let oneList = [];
let oneObj = {
      
    };
for(let i = 0;i<d.length;i++){
    oneObj = {  }
    if(d[i] === str){
        oneObj.text = d[i];
        oneObj.isSelect = true;
        oneObj.isActive = false;
        if(i !== 0) {
            oneObj.startNum = locationList[oneList[i - 1].index].startNum
            oneObj.endNum = locationList[oneList[i - 1].index].endNum
            oneObj.index = oneList[i - 1].index + 1;
        }
        else {
            oneObj.startNum = locationList[0].startNum
            oneObj.endNum = locationList[0].endNum
            oneObj.index = 1
        }
    }else{
        oneObj.text = d[i];
        oneObj.isSelect = false;
        oneObj.isActive = false;
         if(i !== 0) oneObj.index = oneList[i - 1].index;
         else oneObj.index = 0
    }
oneList.push(oneObj);
}
console.log("oneList=================>", oneList);

總結(jié)

到此這篇關(guān)于js如何在字符串中查找某個字符位置的文章就介紹到這了,更多相關(guān)js字符串查找字符位置內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論

龙州县| 桂东县| 郸城县| 永安市| 宝山区| 兴文县| 阿克苏市| 香河县| 肃北| 永康市| 济源市| 缙云县| 镇康县| 屏山县| 六盘水市| 家居| 营口市| 克拉玛依市| 上饶县| 西乡县| 揭东县| 西安市| 逊克县| 桐庐县| 邹城市| 湖北省| 广安市| 宁武县| 柳江县| 安西县| 揭西县| 陵水| 那曲县| 逊克县| 潼关县| 广德县| 永寿县| 中江县| 象山县| 获嘉县| 金坛市|