I got a list of string. I want to do a search inside and return a new list. The search value is a string (sentence). Each word are split by a space.
So, I look at a way to search each string containing each word of the sentence.
sample :
list = {"abcdef", "abc", "ab", "cd ab"}
search "ab" => return list with "abcdef", "abc", "ab", "cd ab"
search "abc" => return list with "abcdef", "abc"
search "ab cd" => return list with "abcdef","cd ab"
its simple but I don't know how to do it with Linq in a single command. Something like
if l.contains(list)
where contains check every element of the list.
That may be simple, I just ask how. Or maybe a link to another post that I have not seen.
Thank you
if (list.Any(w => w.Contains(something))
The tricky one is your last case of "ab cd".
var list = new List<string> {"abcdef", "abc", "ab", "cd ab"};
var result = list.Where (w => "ab cd".Split().All (s => w.Contains(s)));
Click here to see the proof that it catches all cases you've outlined.
Related
Say I have a string of words separated by spaces and commas like
phrase="Hey John, take that"
I would like to split the string into a list using list comprehensions to obtain
["Hey", "John", "take", "that"]
Any suggestion?
I used the Project Gutenberg #29765 English language dictionary file, re-formatted it, and created a few large hashes (A through D, etc) out of it.
I am using a lookup like this which is working fine and displays the definition when you type in a word-
$look = gets.chomp
$look = $look.upcase
puts " "
if (/^[A-D]/).match($look)
puts dicta[$look]
The problem is that some words have more than one entry- rather than extensively modify a 113,000 entry dictionary file, I thought I would just modify the duplicates from this-
dicta = { "BARE" => "def 1", "BARE" => "def 2"}
...to this
dicta = { "1| BARE" => "def 1", "2| BARE" => "def 2"}
I'd add a second lookup of some kind to find these duplicates
elsif (/^[0-9] + {user input}) ... (something?)
What do I need in order to make it find and display both duplicates for BARE?
I strongly suspect this is the complete wrong way to build all of this, but... any help would be much appreciated. Thank you in advance.
this {"BARE" => ["def 1", "def 2"]} seems to be a better data structure than what you have chosen, putting duplicates in a list still makes your hash lookup O1 – bjhaid
#bjhaid (hi bjhaid) has the right idea. If your hash is h and "BARE" has just one definition, you could have either h["BARE"] = ["def 1"] or h["BARE"] = "def ". In the latter case, you wouild need to check if h["BARE"].is_a? Array..<multiple definitions> else...<single definition>...end. – Cary Swoveland
Imagine we have some sequence of letters in the form of a string, call it
str = "gcggcataa"
The regular expression
r = /(...)/
matches any three characters, and when I execute the code
str.scan(r)
I get the following output:
["gcg", "gca", "taa"]
However, what if I wanted to scan through and instead of the distinct, non-overlapping strings as above but instead wanted to get this output:
["gcg", "cgg", "ggc", "gca", "cat", "ata", "taa"]
What regular expression would allow this?
I know I could do this with a loop but I don't want to do that
str = "gcggcataa"
str.chars.each_cons(3).map(&:join) # => ["gcg", "cgg", "ggc", "gca", "cat", "ata", "taa"]
I have the following linq expression to do my ordering but was wondering how do I change this so that it will orderby name but ignore the first word if it is "the"
CaseStudies.OrderBy(a => a.Name)
Simplest way (if there is always lower-case the and no more than one space between words):
CaseStudies.OrderBy(a => a.Name.StartsWith("the ") ? a.Name.Substring(4) : a.Name)
You can create method with nice descriptive name and move this logic as well as null check and ignore case comparison there:
private string RemoveDefiniteArticle(string s)
{
if (String.IsNullOrEmpty(s))
return s;
if (s.StartsWith("the ", StringComparison.CurrentCultureIgnoreCase))
return s.Substring(4).TrimStart();
return s;
}
And use it
CaseStudies.OrderBy(a => RemoveDefiniteArticle(a.Name))
There are a surprising number of edge cases here. Suppose your list is
List<string> strings = new List<string> { "The aardvark", "the bear", "The cat", " dog", " elephant"};
Then the starting point is handling "the" at the start
strings.OrderBy(w => w.StartsWith("the ") ? w.Substring(4) : w);
Which gives:
elephant
dog
the bear
The cat
The aardvark
Ignoring case is better
strings.OrderBy(w => w.StartsWith("the ", StringComparison.CurrentCultureIgnoreCase) ? w.Substring(4) : w);
Giving:
elephant
The cat
dog
The aardvark
the bear
Handling multiple spaces after the leading "the" is even better, but not perfect:
strings.OrderBy(w => w.StartsWith("the ", StringComparison.CurrentCultureIgnoreCase) ? w.Substring(4).TrimStart() : w);
elephant
dog
The aardvark
the bear
The cat
Handling leading spaces before the leading "the" looks correct
strings.OrderBy(w => w.TrimStart().StartsWith("the ", StringComparison.CurrentCultureIgnoreCase) ? w.TrimStart().Substring(4).TrimStart() : w.TrimStart());
Gives:
The aardvark
the bear
The cat
dog
elephant
But there may be other edge cases around null/empty/whitespace checking at multiple points...
CaseStudies.OrderBy(a => a.Name.TrimStart().StartsWith("the ", StringComparison.CurrentCultureIgnoreCase) ? a.Name.TrimStart().Substring(4).TrimStart() : a.Name)
can i do something like this?
string = "123" + rand(10).to_s + "abc"
=> "1237abc"
string.include?("123" + string.any_character(1) + "abc") # any_character(digit)
=> true
the point is to know a part of a string like lets say a html tag string, testing for something like Name Changes Every Day
and easily find the title from the source every time no matter what it might be, but for me, it will always be one character, soooooo, any help here?
Try that code:
string = "123" + rand(10).to_s + "abc"
string =~ /123\dabc/
0 means that pattern starts at 0 character. This return nil when pattern doesnt match. If you want to match only whole text change /123\dabc/ into /^123\dabc$/
You can just use regular expressions with ruby. Example:
string = "123" + rand(10).to_s + "abc"
string.match /123\dabc/