Very long code

by a guest

This is a pretty long code that I found on StackoverFlow. Credit: @Vitim.us

Very long code
1/** Function that count occurrences of a substring in a string;
2 * @param {String} string               The string
3 * @param {String} subString            The sub string to search for
4 * @param {Boolean} [allowOverlapping]  Optional. (Default:false)
5 *
6 * @author Vitim.us https://gist.github.com/victornpb/7736865
7 * @see Unit Test https://jsfiddle.net/Victornpb/5axuh96u/
8 * @see https://stackoverflow.com/a/7924240/938822
9 */
10function occurrences(string, subString, allowOverlapping) {
11
12    string += "";
13    subString += "";
14    if (subString.length <= 0) return (string.length + 1);
15
16    var n = 0,
17        pos = 0,
18        step = allowOverlapping ? 1 : subString.length;
19
20    while (true) {
21        pos = string.indexOf(subString, pos);
22        if (pos >= 0) {
23            ++n;
24            pos += step;
25        } else break;
26    }
27    return n;
28}

✨ Code Explanation ✨

This JavaScript code defines a function `occurrences` that takes three parameters: a string, a substring, and a Boolean value indicating whether overlapping occurrences should be counted. First, the function converts the string and substring to strings if they are not already strings. If the length of the substring is less than or equal to 0, the function returns the length of the string plus 1. Next, the function initializes three variables: `n` to 0 to keep track of the number of occurrences, `pos` to 0 to keep track of the position of the substring, and `step` to either 1 (if overlapping is allowed) or the length of the substring (if not). Then, the function enters a loop that continues until there are no more occurrences of the substring in the string. In each iteration of the loop, the function finds the next occurrence of the substring in the string starting at position `pos` using the `indexOf` method. If an occurrence is found, the counter `n` is incremented and the `pos` variable is updated to the next position depending on the value of `step`. Finally, the function returns the number of occurrences `n`. This function is useful for counting the number of times a particular substring appears in a longer string and can be used for tasks such as parsing text or validating input.

Share

Share
Video
A cool 10 sec video.
Share
Detailed
Title, description, and syntax highlighted code.
Share
Simple
Syntax highlighted code with gradient background colored presentation.

Comments

It looks like there is no comment for this snippet. Leave the first one!
You need to login to send a comment.

Similar Snippets

Google auth login for react
Google auth login for react

Google auth login with react implementation

Language: typescript19 months ago
React Native Scaling for Responsiveness
React Native Scaling for Responsiveness

React Native scaling for responsiveness, Use: style={{ fontSize: vs(34) }}

Language: typescript17 months ago