tags:

views:

73

answers:

3

Hello All,

In C#, Is it possible to read the desired string from a given text file ?

Sample content: aaaaaaaaaaaaaabbbbbcccccc dddddddddeeeeeeeeefffffff gggghhhhhhhhiiiijjjjjjjkk lllmmmmmmmmmmmnnnnnnnnnnn ooooooooooopppppppppppppp

Now I have to read ffffff and iiiiiii and lllll and so on.... Thanks in advance...

+1  A: 

you need to see Regular Expression Classes.

something similar, with modification will do

{([a-zA-Z])\1+)}

also check this resource

Asad Butt
A: 

I think what you are looking for is regular expressions. Regular expressions are a powerful tool to search for specific patterns in strings. You can find much stuff about them on the internet, also have a look at MSDN.

Best Regards,
Oliver Hanappi

Oliver Hanappi
A: 

If that is the exact string you are referring to, you could just enumerate the alphabet and use it as a regex e.g.

using System.Text.RegularExpressions;
using System.IO;
...

char[] alpha = "abcdefjhijklmnopqrstuvwxyz".ToCharArray();
string contents = String.Empty;
using (var file = new StreamReader("MyFile.txt"))
{
     contents = file.ReadToEnd();
}

foreach (var c in alpha)
{
    Match m = new Regex(String.Format("{0}+", c.ToString()), RegexOptions.IgnoreCase).Match(contents);
    if (m != null)
    {
        var str = m.Value;
        // do something with str
    }
}
James
What do you expect to see as m.Value here? And why would you create a regular expression matching a single char?
Benjamin Podszun
I forgot to add the + to the regex. It is so the OP can match the strings in the manner they have asked.
James