TAGS :Viewed: 8 - Published at: a few seconds ago

[ How to Read desired string from a given text file ??? using C# ]

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...

Answer 1

you need to see Regular Expression Classes.

something similar, with modification will do


also check this resource

Answer 2

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

Answer 3

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