A
A
akass2014-11-19 03:07:40
Software testing
akass, 2014-11-19 03:07:40

How to check that the collection contains all strings from the given letters?

There is a method, the input is a collection consisting of strings, how to check that all strings are made of letters, and not numbers or some other nonsense?
PS It turned out that it was necessary that there were not just lines of letters, but of 4 possible letters, tell me how to implement it?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
V
Vitaly Pukhov, 2014-11-19
@Neuroware

for char there is a function IsLetter, you can do it like this:

<code lang="cs">
static void Main(string[] args)
        {
            List<string> s = new List<string>();
            s.Add("asdasdasd");
        }

        string AllIsLetter(List<string> s)
        {
            foreach (string str in s)
            {
                foreach (char ch in str)
                {
                    if (!Char.IsLetter(ch))
                    {
                        return "Не все строки содержат только буквы!";
                    }
                }
            }
            return "В коллекции только буквы";
        }
</code>

B
bmforce, 2014-11-19
@bmforce

With linq:

List<string> list = new List<string> { "sdfsd", "dfgdf", "ssss" };
bool result;
result = list.TrueForAll(s => s.All(c => Char.IsLetter(c)));
Console.WriteLine(result); //true

list = new List<string> { "sdf5", "dfgdf", "ssss" };
result = list.TrueForAll(s => s.All(c => Char.IsLetter(c))); 
Console.WriteLine(result); //false

S
Sergey Mishin, 2014-11-19
@sergeysmishin

class Program
{
    static void Main(string[] args)
    {
        var stringList = new List<string> {"asdasd","","adadasd","фывфывфыв","dвыацwe"};

        Console.WriteLine(CheckList(stringList) ? bool.TrueString : bool.FalseString);
    }
  
    private static bool CheckList(IEnumerable<string> stringList)
    {
        return stringList.All(str => Regex.IsMatch(str, @"\A\p{L}*\Z"));
    }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question