I am working on a javascript code to find the nth occurrence of a character in a string. Using the indexOf()
function we are able to get the first occurrence of the character. Now the challenge is to get the nth occurrence of the character. I was able to get the second third occurrence and so on using the code given below:
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 ... }
The result is given below
First occurence 3 Second occurence 9 Third occurence 10 Fourth occurence 14 Fifth occurence 18 Sixth occurence 19
I would like to generalize the script so that I am able to find the nth occurrence of the character as the above code requires us to repeat the script n times. Let me know if there is a better method or alternative to do the same. It would be nice if we just give the occurrence (at run time) to get the index of that character.
The following are some of my questions:
1) Select Lookup from the drop-down list of Formula Type section; 2) Choose Find where the character appear Nth in a string in Choose a formula section; 3) Select the cell which contains the string you use, then type the specified character and nth occurrence in to the textboxes in the Arguments input section.
To find the index of nth occurrence of a substring in a string you can use String. indexOf() function. A string, say str2 , can occur in another string, say str1 , n number of times. There could be a requirement in your Java application, that you have to find the position of the nth occurrence of str2 in str1 .
JavaScript String indexOf() The indexOf() method returns the position of the first occurrence of a value in a string. The indexOf() method returns -1 if the value is not found. The indexOf() method is case sensitive.
function nth_occurrence (string, char, nth) { var first_index = string.indexOf(char); var length_up_to_first_index = first_index + 1; if (nth == 1) { return first_index; } else { var string_after_first_occurrence = string.slice(length_up_to_first_index); var next_occurrence = nth_occurrence(string_after_first_occurrence, char, nth - 1); if (next_occurrence === -1) { return -1; } else { return length_up_to_first_index + next_occurrence; } } } // Returns 16. The index of the third 'c' character. nth_occurrence('aaaaacabkhjecdddchjke', 'c', 3); // Returns -1. There is no third 'c' character. nth_occurrence('aaaaacabkhjecdddhjke', 'c', 3);
You can do it easily by implementing a function using charAt()
, like this:
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
Thanks to CQQL for let me know what OP really wanted. I updated a bit my original function to achieve the new behaviour.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With