Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find all possible substrings in a string?

Tags:

string

c#

.net

linq

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?

like image 398
Abe Miessler Avatar asked May 23 '12 03:05

Abe Miessler


2 Answers

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 
like image 187
Enigmativity Avatar answered Oct 05 '22 23:10

Enigmativity


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);
}
like image 42
Sergey Kalinichenko Avatar answered Oct 05 '22 23:10

Sergey Kalinichenko