DEV Community

Cover image for Substring and stuff with JavaScript
Vishwa.R
Vishwa.R

Posted on

2 1

Substring and stuff with JavaScript

Sub-string-example

Image source: Wikipedia

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,

// sample string in JS
let sample_string = "Hello readers";
view raw string.js hosted with ❤ by GitHub

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.

// sample string
let sample_string = "wsad";
// substrings of the above string
w
ws
wsa
wsad
s
sa
sad
a
ad
d
view raw Substring.js hosted with ❤ by GitHub

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.

// Initialising input string
let inpstring = "Hello";
// length of the input string
let n = inpstring.length;
view raw Initialising.js hosted with ❤ by GitHub

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.
let inpstring = "abc";
let n = inpstring.length;
function FindSubstring(Str, n){
for(let i=0; i<n ;i++){
// We are going to have two more loops here
}
}
}
}
view raw Function.js hosted with ❤ by GitHub

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,
let inpstring = "abc";
let n = inpstring.length;
function FindSubstring(Str, n){
for(let i=0; i<n ;i++){
for(let j=i;j<n;j++){
// another for loop comes here
}
}
}
view raw Substring.js hosted with ❤ by GitHub

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,

let inpstring = "abc";
let n = inpstring.length;
function FindSubstring(Str, n){
for(let i=0; i<n ;i++){
for(let j=i;j<n;j++){
for(let k=i;k<j+1;k++){
console.log(Str.charAt(k))
}
}
}
}
view raw Substring.js hosted with ❤ by GitHub

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,

let inpstring = "abc";
let n = inpstring.length;
function FindSubstring(Str, n){
for(let i=0; i<n ;i++){
/* console.log(i) */
for(let j=i;j<n;j++){
for(let k=i;k<j+1;k++){
console.log(Str.charAt(k))
}
}
}
}
FindSubstring(inpstring,n)
view raw Substring.js hosted with ❤ by GitHub

Awesome ✨🎉, we've done it.

You can also take a look at this JSFiddle 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 Timothy Muza on Unsplash

SurveyJS custom survey software

Simplify data collection in your JS app with a fully integrated form management platform. Includes support for custom question types, skip logic, integrated CCS editor, PDF export, real-time analytics & more. Integrates with any backend system, giving you full control over your data and no user limits.

Learn more

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay