Amit
Amit

Reputation: 3478

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

Upvotes: 0

Views: 392

Answers (3)

James
James

Reputation: 82136

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

Upvotes: 1

Asad
Asad

Reputation: 21938

you need to see Regular Expression Classes.

something similar, with modification will do

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

also check this resource

Upvotes: 1

Oliver Hanappi
Oliver Hanappi

Reputation: 12346

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

Upvotes: 0

Related Questions