How to know whether a string is a substring of list of strings.
-
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
-
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
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
-
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
-
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; }
+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
-
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
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();}