SitePoint Sponsor

User Tag List

Results 1 to 10 of 10

Hybrid View

  1. #1
    Learning...
    Join Date
    Jan 2003
    Posts
    781
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    C# check array item is in a string

    Checking string in array is simple
    Code:
    string[] array = { "foo", "bar" };
    if (array.Contains("foo")) {
        //...
    }
    I need to check if array item(s) is in string. Is it possible to avoid a loop here?
    Code:
    string[] array = { "foo", "bar" };
     string item = "foolish";
    bool isIitem = item.Contains(array); //looking for some thing similar
    Thanks
    The beauty of life is not dependent on how happy you are,
    but on how happy others can be because of you...

  2. #2
    Non-Member
    Join Date
    Apr 2011
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    there are two ways this can be done, the first is with a loop, the second is using LINQ's "Any" extension method with 1 line of code and a lambda expression:
    Code Csharp:
    string[] haystack = {"hello", "world"};
    string needle = "he";
     
    bool isFound = false;
    foreach (var bale in haystack)
    {
    	if(bale.Contains(needle))
    		isFound = true;
    }
     
    bool doesAnyItemContainThisString = haystack.Any(bale => bale.Contains(needle));

  3. #3
    Community Advisor ULTiMATE's Avatar
    Join Date
    Aug 2003
    Location
    Bristol, United Kingdom
    Posts
    2,160
    Mentioned
    46 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by solevangelist View Post
    bool doesAnyItemContainThisString = haystack.Any(bale => bale.Contains(needle));
    [/HIGHLIGHT]
    Ah, Lambda expressions and LINQ. Is there anything more beautiful on this planet? I think not,

  4. #4
    Non-Member
    Join Date
    Apr 2011
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by ULTiMATE View Post
    Ah, Lambda expressions and LINQ. Is there anything more beautiful on this planet? I think not,
    I couldn't agree more. Despite the ASP.NET + WebForms paradigm having rubbed web developers up the wrong way for quite awhile I think the ASP.NET Web Pages WebMatrix, Razor and .NET 3.5/4.0 and language developments (LINQ, var, automatic properties, automatic properties, collection intializers, named and optional arguments, etc) greatly increase the efficiency and elegance of web development in .NET

  5. #5
    SitePoint Member
    Join Date
    Apr 2011
    Posts
    19
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    List<string> blahList = new List<string>(
    new string[] { "foo", "bar"});
    string item = "foolish";
    bool isIitem = blahList.Contains(item);

  6. #6
    Non-Member
    Join Date
    Apr 2011
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    goldensum's post made me re-read the question (sorry!). I wrote it in reverse. I think what he was trying to say is "does the string contain any of the items in the array". Therefore the needles and the haystack are reversed. Here are the two options:

    Code Csharp:
    string[] needles = {"hello", "world"};
    string haystack = "hello world";
    bool doesHaystackContainAnyNeedles = false;
     
    // LINQ
    doesHaystackContainAnyNeedles = needles.Any (needle => haystack.Contains(needle));
     
    // Loop
    foreach(var needle in needles)
    {
    	if(haystack.Contains(needle))
    		doesHaystackContainAnyNeedles = true;
    }

  7. #7
    SitePoint Author silver trophybronze trophy
    wwb_99's Avatar
    Join Date
    May 2003
    Location
    Washington, DC
    Posts
    10,649
    Mentioned
    4 Post(s)
    Tagged
    0 Thread(s)
    In cases like this, the linq bit will still be doing a loop under the covers -- how else can it find the match where there is no index or other way of cheating?

  8. #8
    Non-Member
    Join Date
    Apr 2011
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes, of course. But somehow I still prefer the elegance of LINQ and though this is a simple example I find it pays dividends in other areas where one ends up using a cleaner more functional programming style rather than mutating variables.

    To take it to the extreme, I occasionally use the .ForEach(x => DoSomething(x)) extension on my List<T> rather than a foreach(var x in y) { DoSomething(x); } I note ReSharper seems to prefer this too). Then when you want to parallelize it, it's as simple as using the Parallel.ForEach method rather than the ForEach extension method. If I want to do a transformation of the data beforehand, I can just use a .Select(x => x) extension method in front of the .ForEach().

  9. #9
    Learning...
    Join Date
    Jan 2003
    Posts
    781
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks.
    The beauty of life is not dependent on how happy you are,
    but on how happy others can be because of you...

  10. #10
    SitePoint Author silver trophybronze trophy
    wwb_99's Avatar
    Join Date
    May 2003
    Location
    Washington, DC
    Posts
    10,649
    Mentioned
    4 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by solevangelist View Post
    Yes, of course. But somehow I still prefer the elegance of LINQ and though this is a simple example I find it pays dividends in other areas where one ends up using a cleaner more functional programming style rather than mutating variables.

    To take it to the extreme, I occasionally use the .ForEach(x => DoSomething(x)) extension on my List<T> rather than a foreach(var x in y) { DoSomething(x); } I note ReSharper seems to prefer this too). Then when you want to parallelize it, it's as simple as using the Parallel.ForEach method rather than the ForEach extension method. If I want to do a transformation of the data beforehand, I can just use a .Select(x => x) extension method in front of the .ForEach().
    No arguments there. Just wanted to dispel the idea that linq was some sort of database like magic that could make straight list lookups faster than O(n) operations . . .


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •