Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. LINQ
  4. How to know whether a string is a substring of list of strings.

How to know whether a string is a substring of list of strings.

Scheduled Pinned Locked Moved LINQ
csharpdatabaselinqregexhelp
5 Posts 5 Posters 16 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    NarVish
    wrote on last edited by
    #1

    I'm trying to know whether test string is substring of sentences list.

    List sentences = new List{"sentence1 word10 word11", "sentence2 word2", "sentence3 word3"};

    Expected output for string, test should be as below: case 1:string test = "word"; it should return false case 2:tring test = "word10"; it should return true case 3:string test = "sentence1 word10"; it should return true case 4:string test = "sentence1 word10 word11"; it should return true I tried the below linq query, but its failing for the test = "word";

    bool isFound = sentences.Any(p => p.Contains(test));

    Hope you understand the problem. complete words of test should match the words in sentences, not part of the word. please correct the query

    W E Y 3 Replies Last reply
    0
    • N NarVish

      I'm trying to know whether test string is substring of sentences list.

      List sentences = new List{"sentence1 word10 word11", "sentence2 word2", "sentence3 word3"};

      Expected output for string, test should be as below: case 1:string test = "word"; it should return false case 2:tring test = "word10"; it should return true case 3:string test = "sentence1 word10"; it should return true case 4:string test = "sentence1 word10 word11"; it should return true I tried the below linq query, but its failing for the test = "word";

      bool isFound = sentences.Any(p => p.Contains(test));

      Hope you understand the problem. complete words of test should match the words in sentences, not part of the word. please correct the query

      W Offline
      W Offline
      Wayne Gaylard
      wrote on last edited by
      #2

      You could use the Find function to see if any of the strings contain the specific word you are looking for, then if you just need to return a boolean then you can just evaluate the Count function, something like this:- To return the string that the word is found in use this:-

      var q = sentences.Find((c) => c.Contains("word2"));

      then if you only need to return the boolean:-

      var q = sentences.Find((c) => c.Contains("word2")).Count() > 0;

      Hope this helps

      When I was a coder, we worked on algorithms. Today, we memorize APIs for countless libraries — those libraries have the algorithms - Eric Allman

      1 Reply Last reply
      0
      • N NarVish

        I'm trying to know whether test string is substring of sentences list.

        List sentences = new List{"sentence1 word10 word11", "sentence2 word2", "sentence3 word3"};

        Expected output for string, test should be as below: case 1:string test = "word"; it should return false case 2:tring test = "word10"; it should return true case 3:string test = "sentence1 word10"; it should return true case 4:string test = "sentence1 word10 word11"; it should return true I tried the below linq query, but its failing for the test = "word";

        bool isFound = sentences.Any(p => p.Contains(test));

        Hope you understand the problem. complete words of test should match the words in sentences, not part of the word. please correct the query

        E Offline
        E Offline
        emardini
        wrote on last edited by
        #3

        Try this:

        private static bool IsStringInList(List<string> sentences, string seek)
        {
        var seekList = seek.Split(' ');
        var plainList = sentences.SelectMany(x => x.Split(' '));
        var result = plainList.Intersect(seekList).Any();

              return result;
          }
        
        B 1 Reply Last reply
        0
        • E emardini

          Try this:

          private static bool IsStringInList(List<string> sentences, string seek)
          {
          var seekList = seek.Split(' ');
          var plainList = sentences.SelectMany(x => x.Split(' '));
          var result = plainList.Intersect(seekList).Any();

                return result;
            }
          
          B Offline
          B Offline
          BillWoodruff
          wrote on last edited by
          #4

          +5 I haven't tested this yet, but I appreciate your sharing such an interesting use of Linq ! thanks, Bill

          "Science is facts; just as houses are made of stones: so, is science made of facts. But, a pile of stones is not a house, and a collection of facts is not, necessarily, science." Henri Poincare

          1 Reply Last reply
          0
          • N NarVish

            I'm trying to know whether test string is substring of sentences list.

            List sentences = new List{"sentence1 word10 word11", "sentence2 word2", "sentence3 word3"};

            Expected output for string, test should be as below: case 1:string test = "word"; it should return false case 2:tring test = "word10"; it should return true case 3:string test = "sentence1 word10"; it should return true case 4:string test = "sentence1 word10 word11"; it should return true I tried the below linq query, but its failing for the test = "word";

            bool isFound = sentences.Any(p => p.Contains(test));

            Hope you understand the problem. complete words of test should match the words in sentences, not part of the word. please correct the query

            Y Offline
            Y Offline
            Yoseph TX
            wrote on last edited by
            #5

            There are several ways to solve this problem with link. I guess u wanted to return a boolean value indicating if the word found or not (of course you can mod the code to return a boolean). I decided to take a different approach that is fairly scalable..not the best code since i hacked it up in a few mins...

            string testWord = "word3";

            // Returns the sentence with the word - this can be modded to return a boolean instead
            var sentenceWithSearchWord = sentences
            .Where(p => Regex.IsMatch(p, testWord)
            .FirstOrDefault();

            // Splits the sentence returned to extract the single word.
            if (sentenceWithSearchWord != null)
            {
            string wordResult = sentenceWithSearchWord
            .Split(' ')
            .Where(p => Regex.IsMatch(p, testWord))
            .First();

            }

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups