Substring and stuff with JavaScript
Tags: todayilearned, javascript, programming
Published: 2021-07-24 18:28:08

![Sub-string-example](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kvx6usqx5b9mbtdtdb2a.png)
<figcaption>Image source: Wikipedia</figcaption>

## What is a string?
A string is a thin wire, you used to hang your clothes to dry. **Just kidding !** 
A string in computer terms is a sequence of characters, we use strings to represent words or a sequence of characters in programming. Here's an example for a string,
{% gist https://gist.github.com/code-reaper08/9190fb4f8576fb68f0cf1cc5d64c4597 %}

## So, What is a substring, then?
A substring is nothing but a string inside a string. But remember, substrings are contiguous!. If it isn't clear, don't worry, we'll get it right. Have a look at the below gist.
{% gist https://gist.github.com/code-reaper08/e9d67c0b271c0458386ad5501a0b8628 %}

### Bonus insight
Let a given string has a length of 5, let us take it as a variable `n`, then the total number of possible substrings is given by,

```Total possible substrings = n*(n+1)/2```
 for example, let us take `n` as 5 as we assumed above, then

`5*(5+1)/2` which turns into `(5*6)/2`, eventually yields the value of `15` (which is the total number of possible substrings for a string of length 5). 

**_⚡Note:_**
Notice that the substrings are contiguous, notice how `wa` or `wd` are not substrings of `wsad`. Only adjoining sequence characters are taken from the string, and are called as substrings.

Now take a deep breath, we are going to dive into the JavaScript ocean🥽

## JavaScript code for slicing substrings
So, let us look at the JavaScript code step-by-step for printing (I mean console logging) all the substrings for a given string.
Let us start by initializing the input string and the length of the input string.
{% gist https://gist.github.com/code-reaper08/e3007ec80627f0ebcd161cf97e7ea43d %}
These two will be passed to a function called `FindSubstring`, which contains 3 nested `for` loops. The first `for` loop is to identify the starting point of the string passed to the function. We use iterator `i` looping from _0 to <n_.
{% gist https://gist.github.com/code-reaper08/83a0d3fc0a18804366996ff8f8ce3a68 %}
After this, we move on to the second `for` loop, where we iterate using iterator `j` from _i to <n_. Using this second `for` loop, we determine the ending point. Take a look at the below gist,
{% gist https://gist.github.com/code-reaper08/49c9234ad14ebb54545b82ec74fa40a6 %}

Now, after determining the starting and ending point of the input string, we use a third `for` loop to `console log` the sequence of characters from starting point to ending point. For that, we loop using iterator `k` from _i to <j+1_. Inside the loop we `console log` the sequence of characters as follows,
{% gist https://gist.github.com/code-reaper08/054bee532a429363a0c94e56db04b2ad %}

We use `charAt` method above to pass in the string index, i.e `k`.
### Do you think it's over? 
## **NO!** 
Not until we call our `FindSubstring` function and pass it both `inpstring` and `n` values. Let us do that to wrap it up,
{% gist https://gist.github.com/code-reaper08/2829421b96383fa3546d3c11dbc7680f %}

## Awesome ✨🎉, we've done it.

You can also take a look at this [JSFiddle](https://jsfiddle.net/Vishwa_R/z81e7h32/) to change input strings to your wish.


Feel free to correct me if I am wrong, give a 💖 if you like the content. Thanks for reading and have a nice day.


### Acknowledgements:
Cover image : Photo by <a href="https://unsplash.com/@timothymuza?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText">Timothy Muza</a> on <a href="https://unsplash.com/s/photos/piece?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText">Unsplash</a>