Compare the first letters of a list - c#

I've a list with some strings like this:
List<String> data = new List<String>
{
"marine",
"blue",
"SEM",
"seven",
"sensible",
"six"
};
Now I want to compare this list with a string and add the matching items to a new list:
String input = "se";
List<String> newList = new List<String>;
The matching condition is, that the first letters should be the same (case-sensitive). In this case the newList contains:
"seven" and "sensible"
How is the most performant solution?

var newList = data.Where(s => s.StartsWith(input)).ToList();

Related

Check if a list<StringCollection> contains list<string>

Given a list<StringCollection> how do most efficiently check whether all string are contained in any of the StringCollections?
Example:
using System;
using System.Collections.Specialized;
using System.Collections.Generic;
public class Program
{
public static void Main()
{
// Create and initializes a new StringCollection.
StringCollection myCol0 = new StringCollection();
StringCollection myCol1 = new StringCollection();
StringCollection myCol2 = new StringCollection();
StringCollection SearchCol = new StringCollection();
// Add a range of elements from an array to the end of the StringCollection.
String[] myArr0 = new String[] { "RED", "car", "boat" };
myCol0.AddRange( myArr0 );
// Add a range of elements from an array to the end of the StringCollection.
String[] myArr1 = new String[] { "Blue", "Goku", "Nappa" };
myCol1.AddRange( myArr1 );
// Add a range of elements from an array to the end of the StringCollection.
String[] myArr2 = new String[] { "Yellow", "Winter", "Summer" };
myCol2.AddRange( myArr2 );
// Add a range of elements from an array to the end of the StringCollection.
String[] myArr3 = new String[] { "Yellow", "Blue", "RED" };
SearchCol.AddRange( myArr3 );
List<StringCollection> a = new List<StringCollection>();
a.Add(myCol0);
a.Add(myCol1);
a.Add(myCol2);
}
}
In this case I want to know whether the strings in SearchCol is contained within the stringcollections stored in List<StringCollection> a
In this case I just like to know which of the searchCol strings is not included in the List<StringCollection> a
The only way I see this it possible to do so is via a double for loop?
Is there any datastructure that would be more efficient rather than an stringcollection?
Is there any datastructure that would be more efficient rather than
an stringcollection
Efficient in what way? Of course you should normally use a IEnumerable<string>(like a string[] vor List<string>) since StringCollection is not generic.
But you can also use StringCollection, you have to cast each item from object to string:
var allStrings = a.SelectMany(c => c.Cast<string>());
var searchStrings = SearchCol.Cast<string>();
bool allSearchStringsAreContained = searchStrings.All(allStrings.Contains);
as for the "how do most efficiently", this simple approach is efficient, but if you have a large list of strings that you search or huge string lists, you could use a set based approach:
HashSet<string> set = new HashSet<string>(searchStrings);
bool allSearchStringsAreContained = set.IsSubsetOf(allStrings);
Finally, if you want to ignore the case, so treat "RED" and "Red" same:
Approach 1:
bool allSearchStringsAreContained = searchStrings.All(s => allStrings.Contains(s, StringComparer.OrdinalIgnoreCase));
Approach 2:
HashSet<string> set = new HashSet<string>(searchStrings, StringComparer.OrdinalIgnoreCase);

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();

CollectionAssert failed to do exact match - need best approach

I'm trying to compare two lists using CollectionAssert but it failed in comparing exact match and also it is not telling which value is incorrect
List<string> ExpectedList = new List<string>() { "apple","orange","grapes","mango"};
List<string> ActualList = new List<string>() { "gova","orange","GRAP"};
CollectionAssert.AreEqual(ExpectedList, ActualList)
Expected results should be in String:
"apple gova, grape GRAP, empty Mango"
How can I do it more efficiently or simply?
Is there any other Assertion available in C#?
Use Zip method like this:
List<string> ExpectedList = new List<string>() {"apple", "orange", "grapes", "mango"};
List<string> ActualList = new List<string>() {"gova", "orange", "GRAP"};
var result = ExpectedList.Zip(ActualList, (first,second) => first != second ?
$"Mismatch = {first} , {second}" : "")
.Concat(ExpectedList.Skip(ActualList.Count))
.Concat(ActualList.Skip(ExpectedList.Count))
.Where(c=>!string.IsNullOrWhiteSpace(c)).ToList();
And if you want to get the result as string:
string theStringVersion = string.Join(",", result);

Combine 2 lists on items where string matches between lists

I have 2 List<string>s that contain a list of network names.
List<string> nets1 = new List<string>() { "net1", "net2", "net3" };
List<string> nets2 = new List<string>() { "net2", "net3", "net4" };
I want to combine them into a new List<string>, but only where the strings are equal. So my desired result will be of type List<string> and contain ONLY net2 and net3.
I have tried to use Union and Concat but they dont seem to be what I am looking for
What you are looking for is Intersect:
var list = list1.Intersect(list2).ToList();
Hope you are looking for the common elements in both list, you can use Intersect
var commonElements = nets1.Intersect(nets2).ToList();

How do I remove all non alphanumeric word from a List<string>

How do I remove all non alphanumeric words from a list of strings (List<string>) ?
I found this regex !word.match(/^[[:alpha:]]+$/) but in C# how can I obtain a new list that contains only the strings that are purely alphanumeric ?
You can use LINQ for this. Assuming you have a theList (or array or whatever) with your strings:
var theNewList = theList.Where(item => item.All(ch => char.IsLetterOrDigit(ch)));
Add a .ToList() or .ToArray() at the end if desired. This works because the String class implements IEnumerable<char>.
Regex rgx = new Regex("^[a-zA-Z0-9]*$");
List<string> list = new List<string>() { "aa", "a", "kzozd__" ,"4edz45","5546","4545asas"};
List<string> list1 = new List<string>();
foreach (var item in list)
{
if (rgx.Match(item).Success)
list1.Add(item);
}
With LINQ + regex, you can use this:
list = list.Where(s => Regex.IsMatch(s, "^[\\p{L}0-9]*$")).ToList();
^[\\p{L}0-9]*$ can recognise Unicode alphanumeric characters. If you want to use ASCII only, ^[a-zA-Z0-9]*$ will work just as well.
There's a static helper function that removes all non-alphanumeric strings from a List:
public static List<string> RemoveAllNonAlphanumeric(List<string> Input)
{
var TempList = new List<string>();
foreach (var CurrentString in Input)
{
if (Regex.IsMatch(CurrentString, "^[a-zA-Z0-9]+$"))
{
TempList.Add(CurrentString);
}
}
return TempList;
}

Categories

Resources