function formatIndexof(str, target, index) {
let begin = index || 0;
let end = str.length || 0;
const tLen = target.length
if (begin > end) {
return -1;
}
if (tLen == 1 || Object.prototype.toString.call(str) === '[object Array]') {
for (let i = begin; i < end; i++) {
if (str[i] === target) {
return i
}
}
} else if (tLen > 1) {
for (let i = begin; i < end; i++) {
const temp = str.slice(i, tLen + i);
if (target === temp) {
return i
}
}
}
return -1;
}
function formatIndexof(str, target, index) {
let begin = index;
let end = str.length - 1 || 0;
if (begin > end) {
return -1;
}
for (let i = begin; i < end; i++) {
if (str[i] === target) {
return i
}
}
return -1;
}
function formatIndexof(str, target, index) {
let begin = index;
let end = str.length - 1 || 0;
if (begin > end) {
return -1;
}
for (let i = begin; i < end; i++) {
if (str[i] === target) {
return i
}
}
return -1;
}
function sIndexOf(str, searchStr, fromIndex = 0){
var regex = new RegExp(`${searchStr}`, 'ig')
regex.lastIndex = fromIndex
var result = regex.exec(str)
return result ? result.index : -1
}
// 测试
var paragraph = 'The quick brown fox jumps over the lazy dog. If the dog barked, was it really lazy?'
var searchTerm = 'dog'
// 测试一:不设置 fromIndex
console.log(sIndexOf(paragraph, searchTerm))
// 40
console.log(paragraph.indexOf(searchTerm));
// 40
// 测试二:设置 fromIndex
console.log(sIndexOf(paragraph, searchTerm, 41))
// 52
console.log(paragraph.indexOf(searchTerm, 41));
// 52
function sIndexOf(str, searchStr, fromIndex = 0){
var regex = new RegExp(`${searchStr}`, 'ig')
regex.lastIndex = fromIndex
var result = regex.exec(str)
return result ? result.index : -1
}
// 测试
var paragraph = 'The quick brown fox jumps over the lazy dog. If the dog barked, was it really lazy?'
var searchTerm = 'dog'
// 测试一:不设置 fromIndex
console.log(sIndexOf(paragraph, searchTerm))
// 40
console.log(paragraph.indexOf(searchTerm));
// 40
// 测试二:设置 fromIndex
console.log(sIndexOf(paragraph, searchTerm, 41))
// 52
console.log(paragraph.indexOf(searchTerm, 41));
// 52
export function indexOf(arr: string, item: string, fromIndex ? : number): number;
export function indexOf(arr: Array < any > , item: any, fromIndex ? : number): number;
export function indexOf(arr: any, item: any, fromIndex: number = 0) {
let index = -1;
if (typeof arr === 'string') {
const m = item.length
for (let i = fromIndex, len = arr.length - m; i < len; i++) {
if (item === arr.substr(i, m)) {
index = i;
break;
}
}
} else if (arr instanceof Array) {
for (let i = fromIndex, len = arr.length; i < len; i++) {
if (item === arr[i]) {
index = i;
break;
}
}
}
return index;
}
function indexOf(a, b){
var index = a.split(b)[0].length;
return index === a.length ? -1 : index;
}
var a = 'abcbbcddc';
var b = 'bb';
indexOf(a, b); // 3
indexOf(a, '1'); // -1
String.prototype.indexOf = function (search, fromIndex = 0) {
if (fromIndex < 0) fromIndex = 0;
if (fromIndex >= this.length) return -1;
let str = this;
let reg = RegExp(`(?<=.{${fromIndex}})(${search})`);
let result = reg.exec(str);
return result ? result.index : -1;
};
function myIndexOf(source, target, start = 0) {
if (Array.isArray(source)) {
let res = -1;
for (let i = start; i < source.length; i++) {
if (source[i] === target) return res = i;
}
}
if (typeof source === 'string') {
const reg = new RegExp(target, 'g');
reg.lastIndex = start;
const result = reg.exec(source);
return result ? result.index : -1;
}
}
Object.prototype.MyIndexOf = function(str, from=0){
let data = this;
let isArray = Array.isArray(data);
let isString = Object.prototype.toString.call(data) == '[object String]';
if (!isArray && !isString) throw new SyntaxError();
let len = data.length;
if(from >= len) return -1;
for(var i = from; i <len; i++){
if (isString){
let strLen = str.length;
if (data.substr(i, i+strLen) == str){
return i;
}
}
if (isArray && data[i] == str){
return i;
}
}
return -1;
}
发布评论
评论(56)
使用正则 indexOf('/:>-|+?','/:>-|+?') 这个测试用例应该通不过
indexOf 字符串,数组也能用吧,
@Zhou-Bill
formatIndexof("test", "tes")
这样不是不行吗好像没说是数组还是字符的indexOf
for循环 这题好像没什么考的意义哦
let str = 'hello wrod'
str.indexOf(str,'ll')
直接凉 兄弟
你赢了
indexOf
有两种:返回从
fromIndex
处开始搜索第一次出现的指定值的索引,如果未找到,返回-1
返回在数组中可以找到一个给定元素的第一个索引,如果不存在,则返回
-1
解答
String.prototype.indexOf()
解题思路:正则,字符串匹配
测试成功
Array.prototype.indexOf()
解题思路:遍历匹配
测试成功
总结一下
你也可以尝试使用遍历匹配法解决
sIndexOf
问题(正则更简洁),这里不做介绍(和aIndexOf
差不多的套路,不同的是,String
类型可以一次匹配多个字符)更过编程算法题可见 JavaScript-Algorithms
只是单字符的
Array.prototype.myIndexOf = function (value, fromIndex) {
const len = this.length
let index = -1
fromIndex = fromIndex === undefined ? 0 : fromIndex
if (fromIndex < 0) {
fromIndex = len + fromIndex;
fromIndex = fromIndex < 0 ? 0 : fromIndex;
}
for (let i = fromIndex; i < len; i++) {
if (this[i] === value) {
index = i;
break;
}
}
return index
}
赞,不过 感觉 SyntaxError 换成 TypeError 是不是更好一点
兼容实现字符串和数组的 indexOf,针对字符串处理方式不一样:
'123'.search(x)
先说下思路吧,比较笨,哈哈
abc
与cccabcaaa
这种比较,就是从第一个开始,每三个长度为一段进行比较abc
比较ccc
,abc
比较cca
,abc
比较cab
... 比较笨吧, 可能性能比较差,但是好理解。。实现
测试
思路一: 利用正则比对当前字符串找出位置
function indexOf1(str,val){
const reg = new RegExp(
${val}
,'gi');const result = reg.exec(str);
return result?result.index:-1;
}
思路二: 利用字符串截取循环找到符合截取值的位置
function indexOf2(str,val){
const sl = str.length;
const vl = val.length;
for(var i=0;i<=sl-vl;i++){
if(str.slice(i,vl+i)===val){
return i;
}
}
return -1;
}
不通过
'CDEFGAB'.sIndexOf('EF') // log -1
看了下评论区发现,此题说用最简单代码实现,故意误导了大家往简单的想...
其实还应考虑以下几点:
1.String.prototype.indexOf 和 Array.prototype.indexOf
2.indexOf第二个可选参数 fromIndex
3.字符串indexOf可以是连续的一段,例如'astring'.indexOf('ing'),实现时无法像处理数组一样 直接拆成一个个字符比较,而应该拆成多段(有多种组合)比较。
数组,感觉这题是考indexOf的定义,fromIndex的各种取值对结果的影响
indexof如果像楼上们那样写的话,速度太慢了,最简单的就是直接用indexof,如果是要实现他,当然是用kmp算法,速度最快。
https://github.com/chengruixin/rax-search/blob/main/src/exactMatch/patternFinder.ts
const NOT_INCLUDE = -1;
String.prototype.indexOf = function(string, start = 0) {
if(typeof this !== 'string') throw new SyntaxError('SyntaxError, needs a string');
const reg = new RegExp(
${string}
, 'ig');reg.lastIndex = start;
const result = reg.exec(this);
return result?.index ?? NOT_INCLUDE;
}
Array.prototype.indexOf = function(item, start = 0) {
if(!item) return NOT_INCLUDE;
if(Array.isArray(this)) throw new SyntaxError('syntax error, needs an array!');
for (let i = start; i < this.length; i++)
}
"hello world".indexOf('he')
有的写得只能匹配单个字符@ghost2113 substr应该换成substring吧
// 暴力匹配 function ViolentMatch(s, p) { var i = 0, j = 0, pLen = p.length, sLen = s.length; while (i < sLen && j < pLen) { if (s[i++] != p[j++]) { i = i - (j - 2) - 1; j = 0; } } return j == pLen ? i - j : -1; }
因为大家都不咋用这个参数,想不起来了呗
Array.prototype.myIndexOf = function(item, start = 0) {
const length = this.length;
const startPos = start >= 0 ? start : length + start;
if (start >= length) return -1;
for (let i = startPos; i < length; i++) {
if (this[i] === item) return i;
}
return -1;
};
function indexOf(str, serchStr, index) {
if(index){
return str.substr(index,).match(serchStr)?str.substr(index,).match(serchStr).index + index :-1
}else{
return str.match(serchStr)?str.match(serchStr).index:-1
}
}
看了一圈怎么没人考虑indexOf的第二个起始参数呢?
function _indexOf(str, reg, fromIndex = 0) { let index = fromIndex; const { length } = str; if (index <= 0) index = 0; if (index >= length) return -1; const arr = str.slice(fromIndex).split(reg); return arr.length > 1 ? arr[0].length + fromIndex : -1 }
这题考点在哪啊,没盖特到 ,难道是有什么特别6的写法吗
能使用其它数组api?
能的话,嘿嘿嘿:
字符串变为数组,用数组方法findIndex去查找。