C# Return match found with Contains - c#

I need to return all matches found when comparing a block of text with a list of strings.
if(myList.Any(myText.Contains))
I can verify that there is a match with the above, but I'm not sure how to go further and return the matching string. I looked into Intersect, but as far as I understood it only works on two of the same type.
Sample data:
myList[] = { "City of London", "City of Edinburgh" }; etc
myText = "I am applying for the position in the City of London";
The desired result here would be "City of London", either via setting the resulting match as a string, or returning the index of myList. Any help greatly appreciated, thanks!

This should work.
List<string> myList = new List<string>();
myList.Add("City of London");
myList.Add("City of Edinburgh");
string myText = "I am applying for the position in the City of London";
var result = myList.Where(x => myText.Contains(x)).ToList();

try this:
string result= myList.FindAll(x=> myText.IndexOf(x)>-1);

var matches = myList.Where(a => myText.IndexOf(a) > 0).ToList();

Related

Can I sort a List<string> by integers inside of them?

For example;
List<string> list = new List<string>{
"1[EMPTY]", "2[EMPTY]", "3[EMPTY]", "4[EMPTY]", "5[EMPTY]", "6[EMPTY]", "7[EMPTY]", "8[EMPTY]", "9[EMPTY]", "10[EMPTY]", "11[EMPTY]", "12[EMPTY]"
};
When I use
list.Sort();
Output:
1[EMPTY] 10[EMPTY] 11[EMPTY] 12[EMPTY] 2[EMPTY] 3[EMPTY] 4[EMPTY] 5[EMPTY] 6[EMPTY] 7[EMPTY] 8[EMPTY] 9[EMPTY]
I want 1-2-3-4-5-6-7-8-9-10-11-12.
How can i solve this problem?
(Sorry my English is bad :{)
You can use OrderBy. Basically trick is to sort the string so parsing as int. and getting the value till the first occurance of [.
List<string> list = new List<string>{
"1[EMPTY]", "2[EMPTY]", "3[EMPTY]", "4[EMPTY]", "5[EMPTY]", "6[EMPTY]", "7[EMPTY]", "8[EMPTY]", "9[EMPTY]", "10[EMPTY]", "11[EMPTY]", "12[EMPTY]"
};
list = list.OrderBy(c => int.Parse(c.Substring(0, c.IndexOf('[')))).ToList();

How can I only store the second part of a string in a list?

I have a list Textlist1 that contains only strings. Each item in the list begins with the same text: "sentence.text.", but I only want to store the second part of the string in another list, I don't want to store the first part "sentence.text." in the list Textlist2.
For example:
List<string> Textlist1 = new List<string>();
List<string> Textlist2 = new List<string>();
The full strings are stored in Textlist1.
Textlist1[0] = "sentence.text.My name is Fred"
Textlist1[1] = "sentence.text.Jenny is my sister"
How can I only add "My name is Fred" and "Jenny is my sister" to Textlist2?
The result should be like this:
Textlist2[0] = "My name is Fred"
Textlist2[1] = "Jenny is my sister"
See online result: https://dotnetfiddle.net/MpswLZ
List<string> Textlist1 = new List<string>() {
"sentence.text.My name is Fred",
"sentence.text.Jenny is my sister"
};
List<string> Textlist2 = new List<string>();
Textlist2 = Textlist1.Select(item => item.Split('.')[2]).ToList();
You can use Linq. (You need do add using System.Linq)
Textlist2= Textlist1
.Select(i=> i.Substring("sentence.text.".Length))
.ToList();
Split the input strings by the periods, limiting the split to 3. Then take the last entry from the array that split produces.
Textlist2[0] = Textlist1[0].Split('.', 3)[2];
Textlist2[1] = Textlist1[1].Split('.', 3)[2];
You could use Regex.Replace the text.
var regex = new Regex("^sentence.text.",RegexOptions.Compiled);
Textlist2.AddRange(Textlist1.Select(x=>regex.Replace(x,string.Empty)));
The "^" in Regex ensure the required text ("sentence.text") is matched only at the beginning of string and not else where.
Sample Input
Sample Output
List<string> Textlist1 = new List<string>
{
"sentence.text.a",
"sentence.text.b"
};
string prefix = "sentence.text.";
List<string> Textlist2 = Textlist1.Select(x => x.Substring(prefix.Length)).ToList();
https://dotnetfiddle.net/oFhvfs
There are ultimately many ways of doing this, this is just one solution. Which approach you choose depends on how complex the pattern replacement is. I've gone with a simplistic approach as your example is a very simple case.

what is this line of code does? "description = ((string[])result[0])[2];"

I have a sql database and I want to use specific column of it. Code below shows the matches at the third column I just want to know what exactly ((string[])result[0])[2] does in the code.
Note: "SingleSelectWhere" function choose those records that match the word of "bag" in the "word" column.
db.OpenDB("English.db");
ArrayList result = db.SingleSelectWhere("petdef", "*", "word", "=", "'bag'");
if(result.Count > 0)
{
description = ((string[])result[0])[2];
}
db.CloseDB();
If you don't know what code does, just try to split it into some more "readable" code. If we take this line: description = ((string[])result[0])[2]; we can do:
var result1 = result;
var result2 = result[0];
var result3 = (string[])result2;
var description = result3[2].
If you set a breakpoint to the first line, just start debugging and see what every step does / the variable contains. Just as a tip.
The answer: it takes the array/list with the name result and return the first element. Than you cast it to a string-array and finally select the thrid element (zero based index!). Hope this helps.

Filter list with linq for similar items

I have a list in which I filter, according to the text input in a TextBox in Xaml. The code below filters the List stored in the results variable. The code checks if the textbox input,ie, queryString, matches the Name of any item in the results list EXACTLY. This only brings back the items from the list where the string matches the Name of a the item exactly.
var filteredItems = results.Where(
p => string.Equals(p.Name, queryString, StringComparison.OrdinalIgnoreCase));
How do I change this so that it returns the items in the list whose Name, is similar to the queryString?
To describe what I mean by Similar:
An item in the list has a Name= Smirnoff Vodka. I want it so that if "vodka" or "smirnoff" is entered in the textbox, the the item Smirnoff Vodka will be returned.
As it is with the code above, to get Smirnoff Vodka returned as a result, the exact Name "Smirnoff Vodka" would have to be entered in the textbox.
It really depends on what you mean, by saying "similar"
Options:
1) var filteredItems = results.Where( p => p.Name != null && p.Name.ToUpper().Contains(queryString.ToUpper());
2) There is also also known algorithm as "Levenshtein distance":
http://en.wikipedia.org/wiki/Levenshtein_distance
http://www.codeproject.com/Articles/13525/Fast-memory-efficient-Levenshtein-algorithm
The last link contains the source code in c#. By using it you cann determine "how close" the query string to the string in your list.
Try this:
fileList.Where(item => filterList.Contains(item))
Try this:
var query = "Smirnoff Vodka";
var queryList = query.Split(new [] {" "}, StringSplitOptions.RemoveEmptyEntries);
var fileList = new List<string>{"smirnoff soup", "absolut vodka", "beer"};
var result = from file in fileList
from item in queryList
where file.ToLower().Contains(item.ToLower())
select file;

SQL Like search in LIST and LINQ in C#

I have a List which contains the list of words that needs to be excluded.
My approach is to have a List which contains these words and use Linq to search.
List<string> lstExcludeLibs = new List<string>() { "CONFIG", "BOARDSUPPORTPACKAGE", "COMMONINTERFACE", ".H", };
string strSearch = "BoardSupportPackageSamsung";
bool has = lstExcludeLibs.Any(cus => lstExcludeLibs.Contains(strSearch.ToUpper()));
I want to find out part of the string strSearch is present in the lstExcludedLibs.
It turns out that .any looks only for exact match. Is there any possibilities of using like or wildcard search
Is this possible in linq?
I could have achieved it using a foreach and contains but I wanted to use LINQ to make it simpler.
Edit: I tried List.Contains but it also doesn't seem to work
You've got it the wrong way round, it should be:-
List<string> lstExcludeLibs = new List<string>() { "CONFIG", "BOARDSUPPORTPACKAGE", "COMMONINTERFACE", ".H", };
string strSearch = "BoardSupportPackageSamsung";
bool has = lstExcludeLibs.Any(cus => strSearch.ToUpper().Contains(cus));
Btw - this is just an observation but, IMHO, your variable name prefixes 'lst' and 'str' should be ommitted. This is a mis-interpretation of Hungarian notation and is redundant.
I think the line should be:
bool has = lstExcludeLibs.Any(cus => cus.Contains(strSearch.ToUpper()));
Is this useful to you ?
bool has = lstExcludeLibs.Any(cus => strSearch.ToUpper().Contains(cus));
OR
bool has = lstExcludeLibs.Where(cus => strSearch.ToUpper().IndexOf(cus) > -1).Count() > 0;
OR
bool has = lstExcludeLibs.Count(cus => strSearch.ToUpper().IndexOf(cus) > -1) > 0;

Categories

Resources