Home > database >  How to display the strings from a list of strings that contain all the chars from a char array? c#
How to display the strings from a list of strings that contain all the chars from a char array? c#

Time:08-25

I have a list of strings and a char array of letters. And I want to display the strings that contain all the chars from the array in the string.

List-list of strings charsArray-array of chars Final-the final list with the sorted words

Example

charsArray  {'a','e','i','z'}
List {"abcde","iertaz","aio","zzeml","amoioze"}
Final(output)  iertaz amoioze

I tried this

foreach(string word in List)
{
    bool contain = true;
    foreach (char letter in charsArray}
    {
        if (word.Contains(letter) && contain)
        {
            Final.Add(word);
        }
        else
        {
            contain = false;

        }

    }

}
foreach (string word in Final)
{
    Console.WriteLine(word);
}

CodePudding user response:

Having

char[] chars = new char[] {
  'a','e','i','z'};

List<string> list = new List<string>() {
  "abcde", "iertaz", "aio", "zzeml", "amoioze" };

We can try subtracting word from chars: if all characters from chars are removed, the word is what we want:

using System.Linq;

...

var result = list
  .Where(word => !chars.Except(word).Any())
  .ToList();

CodePudding user response:

This solution uses All:

var charsArray = new char[] { 'a', 'e', 'i', 'z' };
var L = new[] { "abcde","iertaz","aio","zzeml","amoioze"}.ToList();

var final = L.Where(word => charsArray.All(a => word.Contains(a))).ToList();

The result is correct:
iertaz
amoioze

CodePudding user response:

How about this, demonstrated here

public static IEnumerable<IEnumerable<T>> ContainsAll<T>(
                                            this IEnumerable<IEnumerable<T>> source,
                                            IEnumerable<T> search,
                                            IEqualityComparer<T> comparer = default)
{
    ArgumentNullException.ThrowIfNull(source);
    ArgumentNullException.ThrowIfNull(search);
    comparer ??= EqualityComparer<T>.Default;
    
    var searchSet = search is HashSet<T> ss && ss.Comparer == comparer
        ? ss
        : new HashSet<T>(search, comparer);

    return source.Where(item => searchSet.IsSubsetOf(item));
}

That you can use like this,

var words = new[] {"abcde","iertaz","aio","zzeml","amoioze"};
var search = new[] {'a','e','i','z'};
foreach (string match in words.ContainsAll(search))
{
    Console.WriteLine(match);
}

CodePudding user response:

In this line you are adding the word if the first character is in the word because contain is true

...
if (word.Contains(letter) && contain)
        {
            Final.Add(word);
        }
...

you are almost there, you just need a little modification to avoid the mistake above

foreach(string word in List)
{
    bool contain = true;
    foreach (char letter in charsArray}
    {
        if (!word.Contains(letter))
        {
            contain = false;
        }
    }

  if (contain) 
  {
    Final.Add(word);
  }

}
foreach (string word in Final)
{
    Console.WriteLine(word);
}

CodePudding user response:

You want to find strings that contain all the chars from charsArray. It means every char in charsArray should be contained by the target string.

using System.Linq;

var List = new List<string>() { "abcde", "iertaz", "aio", "zzeml", "amoioze" };
var charsArray = new char[] { 'a','e','i','z' };
var result = List.FindAll(word => charsArray.All(letter => word.Contains(letter)));
  •  Tags:  
  • c#
  • Related