How to use StartsWith with an array of string? - linq

Suppose I have an array of strings:
var array = new string[] {"A", "B"}.
Then I want check if the following string: "boca" starts with the letter included in the array.
What I did is:
var result = "boca".StartsWith(array);
but the method doesn't not accept an arra as argument but a single string

You have to loop the array and check if the word starts with anything in the array. Something like this:
var result = array.Any(s => "boca".StartsWith(s));
Assuming your array is {"A", "B"}, then result will be false, because StartsWith is case-sensitive by default.
If you want it case-insensitive, then this will work:
var result = array.Any(s => "boca".StartsWith(s, StringComparison.CurrentCultureIgnoreCase));
In this case, result will be true.

Related

How to display array of split elements using LINQ

I have this simple code
string[] sequences = {"red,green,blue","orange","white,pink"};
var allWords = sequences.Select(s => s.Split(','));
foreach (var letter in allWords)
{
Console.WriteLine(letter);
}
The problem is that in output I get System.String[] insted of splitted array.
How to display result at console?
Use SelectMany if you want an array of strings and not an array of arrays of strings.
See https://dotnetfiddle.net/0vsjfN
SelectMany concatenates the lists, which are generated by using .Split(','), into a single list.

Compare arrays together with different array types

I want to compare if 2 arrays are equal, here is my code:
var letteronloc = [String]();
letteronloc.append("test")
let characters = Array("test")
if(letteronloc == characters) {
}
but i have an error: could not find an overload for == that accepts the supplied arguments
I think its because the arrays are not equal, because the second array is not an string array. But how can i fix this?
let characters = Array("test") treats the string as a sequence
(of characters) and creates an array by enumerating the elements of the sequence.
Therefore characters is an array of four Characters,
the same that you would get with
let characters : [Character] = ["t", "e", "s", "t"]
So you have two arrays of different element types and that's why
you cannot compare them with ==.
If you want an array with a single string "test" then write it as
let characters = ["test"]
and you can compare both arrays without problem.
You just need to specify the type of the second array:
var letteronloc = [String]();
letteronloc.append("test")
let characters: [String] = Array(arrayLiteral: "test")
if (letteronloc == characters) {
}

Overlapping string matching using regular expressions

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"]

How to check whether an array contains a substring within a string in Ruby?

I have an array of strings:
string_array = ['memberid', 'membershiptype', 'date']
Now I need to check whether or not this array contains 'id', and I want it to return true.
I know I can do string_array.include? 'memberid', but I need to find a substring within that string of 'id'.
string_array.any? { |e| e.include? 'id' }
any? returns true if any of the elements in the array are true for the given predicate (i.e. the block passed to any?)

Ruby Strings - Checking against a set of strings to match

I am trying to check an array of strings for containing one or more matching strings.
Currently I am doing this by using if statements - not nice, but it works - However now I am looking for a more Ruby-like way to do this.
row[:datapoints].each do |data|
if data[:direction].include? "Beusselstr"
data[:image] = "category-1"
end
if data[:direction].include? "Ostkreuz"
data[:image] = "category-1"
end
if data[:direction].include? "Westend"
data[:image] = "category-2"
end
if data[:direction].include? "1)S Gr"
data[:image] = "category-3"
end
end
Instead of this I'd like to store the matching strings in an array. To make it a bit more complicated I actually have different categories of matching terms with their own result actions (see category specific assignment of the data[:image] value).
category_1_keywords = ["Beusselstr","Ostkreuz"]
category_2_keywords = ["nefeld Bhf","Greifswalder","Westend"]
category_3_keywords = ["1)S Gr"]
imagecategories = {:category_1 => category_1_keywords,:category_2 => category_2_keywords,:category_3 => category_3_keywords}
How would filtering the array (row[:datapoints]) using such a matching array (imagecategories) look like?
You would probably want to use the array intersection operator & and check if it's empty.
if (data[:direction] & category_1_keywords).any?
data[:image] = "category-1"
end
4 if's in a row though start looking like time for an iterator:
keywords = {
'category_1' => ["Beusselstr","Ostkreuz"],
'category_2' => ["nefeld Bhf","Greifswalder","Westend"],
'category_3' => ["1)S Gr"]
}
data[:image] = keywords.find{|k,v| (data[:direction] & v).any?}[0]
Assuming that data[:direction] is a string, you can find the category with:
data[:image], _ = imagecategories.find { |category, keywords|
keywords.any? { |keyword|
data[:direction].include?(keyword)
}
}
In plain text: find the first category-keywords pair (imagecategories.find) having any keyword (keywords.any?) that the data[:direction] string contains.

Resources