DEV Community

ZeeshanAli-0704
ZeeshanAli-0704

Posted on • Edited on

Get All Substrings

var theString = "abc",
  allSubstrings = [];

getAllSubstrings(theString);

function getAllSubstrings(str) {
  for (let i = 0; i < str.length; i++) {
    for (let j = i + 1; j <= str.length; j++) {
      allSubstrings.push(str.substring(i, j));
    }
  }
}

console.log(allSubstrings);

Enter fullscreen mode Exit fullscreen mode

Top comments (0)