js如何在字符串中查找某個字符的位置
可以使用 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)文章
老生常談document.ready和window.onload
這篇文章主要介紹了document.ready和window.onload的相關(guān)知識,包括document.ready和window.onload的區(qū)別,要使用document.ready()或者document.onload()的原因分析,本文結(jié)合實例代碼給大家介紹的非常詳細(xì),需要的朋友參考下吧2024-01-01
JavaScript數(shù)組及常見操作方法小結(jié)
這篇文章主要介紹了JavaScript數(shù)組及常見操作方法,結(jié)合實例形式總結(jié)分析了JavaScript數(shù)組的基本獲取、添加、刪除、排序、翻轉(zhuǎn)等相關(guān)操作技巧,需要的朋友可以參考下2019-11-11
微信小程序頁面跳轉(zhuǎn)功能之從列表的item項跳轉(zhuǎn)到下一個頁面的方法
這篇文章主要介紹了微信小程序頁面跳轉(zhuǎn)功能之從列表的item項跳轉(zhuǎn)到下一個頁面的方法,結(jié)合具體實例形式總結(jié)分析了微信小程序頁面跳轉(zhuǎn)及列表item項跳轉(zhuǎn)頁面的相關(guān)操作技巧,需要的朋友可以參考下2017-11-11
JS 組件系列之Bootstrap Table的凍結(jié)列功能徹底解決高度問題
這篇文章主要介紹了JS 組件系列之Bootstrap Table的凍結(jié)列功能徹底解決高度問題,需要的朋友可以參考下2017-06-06

