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?
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 
                        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);
}
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With