How to find all possible substrings in a string?

10,056

Solution 1

How's this for a simple, readable approach?

var text = "welcome";

var query =
    from i in Enumerable.Range(0, text.Length)
    from j in Enumerable.Range(0, text.Length - i + 1)
    where j >= 2
    select text.Substring(i, j);

It produces:

we 
wel 
welc 
welco 
welcom 
welcome 
el 
elc 
elco 
elcom 
elcome 
lc 
lco 
lcom 
lcome 
co 
com 
come 
om 
ome 
me 

Solution 2

This LINQ solution should work:

var str = "welcome";
var items = Enumerable
    .Range(0, str.Length)
    .SelectMany(i => Enumerable.Range(2, str.Length-i-1).Select(j => str.Substring(i, j)))
    .Distinct()
    .OrderBy(s => s.Length);
foreach (var s in items) {
    Console.WriteLine(s);
}
Share:
10,056
Abe Miessler
Author by

Abe Miessler

Software Engineer who works with Javascript/Node.js, Python, C#, Go, SQL Server, MongoDB, MySQL and a whole lot more. I enjoy learning new technologies when they are the best tool for the job. I usually fill the role of a full stack engineer but always seem to enjoy working with data the most. 80th recipient of the Gold SQL badge 50th recipient of the Gold SQL Server badge Hobbies include web application security and machine learning.

Updated on June 04, 2022

Comments

  • Abe Miessler
    Abe Miessler almost 2 years

    What I would like to do is take a string and return all possible substrings that are greater than length 2. So using the welcome example:

    we
    el
    lc
    co
    me
    wel
    elc
    lco
    com
    ome
    welc
    elco
    lcom
    come
    and so on.....
    

    The only way I could think to do it was something like this (totally untested):

    for (int i = 0; i < word.Length; i++) //i is starting position
    {
       for (int j = 2; j + i < word.Length; j++) //j is number of characters to get
       {
           wordList.Add(word.SubString(i, j));
       }
    }
    

    But I'm wondering if there a better way to do this (using LINQ possibly) that I don't know about?