Goober
Goober

Reputation: 13508

linq over strings C#

Could someone please give an example of how to use ling to query over a long string of text and find a substring within that string?

regards

Upvotes: 0

Views: 3999

Answers (3)

Martin
Martin

Reputation: 5452

Making a leap here, but if you want to find a word within a long string and pick it out based on some criteria using LINQ, you could do something like this...

private static string longString = "This is a really long string";
static void Main(string[] args)
{
    var query = from word in longString.Split(' ')
                where word.StartsWith("r")
                select word;
}

I'm saying nothing about whether LINQ is an appropriate technology here.

Upvotes: 2

Hans Malherbe
Hans Malherbe

Reputation: 3018

static void Main(string[] args)
{
    var found = "Where in the world is Carmen Sandiego".Split(' ').Where(part => part.StartsWith("i"));
    foreach (var part in found)
        Console.WriteLine(part);
}

Upvotes: 3

Andrew Hare
Andrew Hare

Reputation: 351456

I wouldn't use LINQ, I would use String.Substring, String.IndexOf, or a regular expression.

Can you post an example of the string you would like to search and an example of a substring you would like to find within that string?

Upvotes: 17

Related Questions