To find all substrings in a given string

Suppose a long string is given, I want to find all substrings has a fixed length.
For example,
string line = "abcdefghabcdegfh5678jhqwi5436";

Open in new window

find substrings that have length 5.
The results are
{"abcde","bcdef","cdefg",..."fh567"...};

Thanks!
zhshqzycAsked:
Who is Participating?
 
DhaestCommented:
Below an example with a very easy loop ....
string line = "abcdefghabcdegfh5678jhqwi5436";
            List<string> lineList = new List<string>();

            for (int i = 0; i < line.Length-5+1; i++)
            {
                lineList.Add(line.Substring(i,5));
            }

Open in new window

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.