How to find all possible substrings in a string?

.net, c#, linq, string

Solution

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 

Problem

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?

Original source