How to use String Concatenation In Javascript
Syntax:
sub +=str[j];
Example: In this example, we will be printing all substrings of a given string in JavaScript using String Concatenation.
let str = "abcd";
let uniqueSub = new Set();
for (let i = 0; i < str.length; i++) {
let substring = "";
for (
let j = i;
j < str.length;
j++
) {
substring += str[j];
uniqueSub.add(substring);
}
}
uniqueSub.forEach((substring) => {
console.log(substring);
});
Output
a ab abc abcd b bc bcd c cd d
JavaScript Program to Print all Substrings of a Given String
In this article, we will see how we can print all substrings of a given string in JavaScript language. We have provided an input string, from which we need to print all the possible substrings. Below we have stated the example for a better understanding of the problem statement:
Example:
Input: abc
Output: a
ab
abc
b
bc
c
To print all substrings of a given string in JavaScript, we have three different approaches, which we have stated below:
So, we will see all of the above approaches with its implementation:
Table of Content
- Using substring() Method
- Using slice() Method
- Using String Concatenation
- Using Array or No Builtin Functions
- Using Recursion