views:

405

answers:

3

Hello! 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

+17  A: 

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?

Andrew Hare
+2  A: 
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);
}
Hans Malherbe
+2  A: 

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.

Martin