在javascript中查找字符串中第n次出现的字符
我正在编写一个javascript代码,以查找字符串中出现的第n个字符。使用在javascript中查找字符串中第n次出现的字符,javascript,Javascript,我正在编写一个javascript代码,以查找字符串中出现的第n个字符。使用indexOf()函数,我们可以获得字符的第一次出现。现在的挑战是获得角色的第n次出现。使用下面给出的代码,我能够获得第二次和第三次出现,以此类推: function myFunction() { var str = "abcdefabcddesadfasddsfsd."; var n = str.indexOf("d"); document.write("First occurence " +n );
indexOf()
函数,我们可以获得字符的第一次出现。现在的挑战是获得角色的第n次出现。使用下面给出的代码,我能够获得第二次和第三次出现,以此类推:
function myFunction() {
var str = "abcdefabcddesadfasddsfsd.";
var n = str.indexOf("d");
document.write("First occurence " +n );
var n1 = str.indexOf("d",parseInt(n+1));
document.write("Second occurence " +n1 );
var n2 = str.indexOf("d",parseInt(n1+1));
document.write("Third occurence " +n2 );
var n3 = str.indexOf("d",parseInt(n2+1));
document.write("Fourth occurence " +n3);
// and so on ...
}
结果如下所示
First occurence 3
Second occurence 9
Third occurence 10
Fourth occurence 14
Fifth occurence 18
Sixth occurence 19
我想概括一下脚本,这样我就能够找到角色的第n次出现,因为上面的代码要求我们将脚本重复n次。让我知道是否有更好的方法或替代方法来做同样的事情。如果我们只给出事件(在运行时)以获取该字符的索引,那就太好了
以下是我的一些问题:
- 我们如何在JavaScript中实现它
- 是否有任何框架提供了以更简单的方式实现相同的功能,或者在其他框架/语言中实现相同功能的替代方法是什么
charAt()
实现一个函数,您可以很容易地做到这一点,如下所示:
function nth_ocurrence(str, needle, nth) {
for (i=0;i<str.length;i++) {
if (str.charAt(i) == needle) {
if (!--nth) {
return i;
}
}
}
return false;
}
alert( nth_ocurrence('aaaaacabkhjecdddchjke', 'c', 3) );//alerts 16
功能第n次出现(str,针,第n次){
for(i=0;iindexOf)接受第二个参数,即字符串中的字符索引以开始搜索
function nthChar(string, character, n){
var count= 0, i=0;
while(count<n && (i=string.indexOf(character,i)+1)){
count++;
}
if(count== n) return i-1;
return NaN;
}
var s= 'abcbbasdbgasdnnaabaasdert';
nthChar(s,'a',7);
函数nthChar(字符串,字符,n){
var计数=0,i=0;
while(count这样做的一个好方法是扩展string类,如下所示:
(function() {
String.prototype.nthOccurrenceIndex = function(charToMatch, occurrenceIndex) {
var char, index, matches, _i, _len;
matches = 0;
index = 0;
for (_i = 0, _len = this.length; _i < _len; _i++) {
char = this[_i];
if (char === charToMatch) {
matches += 1;
if (matches === occurrenceIndex) {
return index;
}
}
index += 1;
}
return -1;
};
}).call(this);
现在你可以做一些事情,比如:
“abcabc”。发生率指数('a',1)
#->0
“abcabc”。发生率指数('a',2)
#->3
“abcabc”。发生率指数('a',3)
#->-1
函数nthIndexOf(搜索,n){
var myArray=[];
对于(变量i=0;i一个可能更清晰的函数。递归并复制索引的机制
:
- 如果第n个数字不正确,则不会导致错误(即简单地将代码转换为循环。
str.charAt(i)
与str[i]相同
-但是你的代码计算出现次数,它找不到第n次出现。是的,名称“n\u ocurrence”与函数所做的并不准确,但它是OP在描述他想要什么时使用的名称,我认为这是我的函数所做的,所以我保留了该名称以与OP的命名一致。因此,让我们总结一下:你哈我创建了一个函数,它的名称是OP引入的,但它没有达到预期的效果,也没有解决OP的问题。这个函数很好。@CQL感谢您向我澄清OP真正想要的是什么,我只是修改了一点我的函数以实现预期的行为。您可能想检查它,因为它使用了比您更简单的方法。@myfashionhub这是if(nth-1==0)
的快捷方式。当我使用上述代码使用警报显示事件时,您也可以在此处了解增量前和增量后运算符。我得到未定义的输出(对于第一次事件以外的任何值)问题是javascript解释器可以插入“;”在他们认为合适的地方。因此他们在返回后放置一个,因为返回值在下一行,因此无法识别。返回字符串不在一行中,因此它显示未定义的输出。这是我的错误。它起作用了,谢谢。@cql如果第n次出现不存在,则此操作失败-我希望它在中返回-1
此实例返回[length up to previous index]-1
-将编辑您的答案以进行修复
(function() {
String.prototype.nthOccurrenceIndex = function(charToMatch, occurrenceIndex) {
var char, index, matches, _i, _len;
matches = 0;
index = 0;
for (_i = 0, _len = this.length; _i < _len; _i++) {
char = this[_i];
if (char === charToMatch) {
matches += 1;
if (matches === occurrenceIndex) {
return index;
}
}
index += 1;
}
return -1;
};
}).call(this);
String.prototype.nthOccurrenceIndex = (charToMatch, occurrenceIndex)->
matches = 0
index = 0
for char in @
if char is charToMatch
matches += 1
return index if matches is occurrenceIndex
index += 1
-1
function nthIndexOf(search, n) {
var myArray = [];
for(var i = 0; i < myStr.length; i++) {
if(myStr.slice(i, i + search.length) === search) {
myArray.push(i);
}
}
return myArray[n - 1];
}