Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,884 questions

51,810 answers

573 users

How to use Array.Exists<T> method to check whether any string in string array begin with a specified character in C#

2 Answers

0 votes
using System;

namespace ConsoleApplication_C_Sharp
{
    class Program
    {
        static void Main(string[] args)
        {
            string[] s = { "PHP", "C", "C++", "C#", "Java", "JavaScript",
                           "VB.NET", "VB6", "Pascal", "Python"};

            Char[] characters = { 'C', 'J', 'V', 'P', 'D'};

            foreach (var ch in characters)
                Console.WriteLine("One or more strings begin with '{0}': {1}",
                                  ch,
                                  Array.Exists(s, (new SearchString(ch)).StartsWith));
        }
    }
    public class SearchString
    {
        Char firstChar;

        public SearchString(Char firstChar)
        {
            this.firstChar = Char.ToUpper(firstChar);
        }

        public bool StartsWith(String s)
        {
            if (String.IsNullOrEmpty(s)) return false;

            if (s.Substring(0, 1).ToUpper() == firstChar.ToString())
                return true;
            else
                return false;
        }
    }
}


/*
run:
 
One or more strings begin with 'C': True
One or more strings begin with 'J': True
One or more strings begin with 'V': True
One or more strings begin with 'P': True
One or more strings begin with 'D': False

*/

 



answered Apr 16, 2016 by avibootz
0 votes
using System;

namespace ConsoleApplication_C_Sharp
{
    class Program
    {
        static void Main(string[] args)
        {
            string[] str = { "PHP", "C", "C++", "C#", "Java", "JavaScript",
                             "VB.NET", "VB6", "Pascal", "Python" };

            Char[] characters = { 'C', 'J', 'V', 'P', 'D'};

            foreach (var ch in characters)
                Console.WriteLine("One or more strings begin with '{0}': {1}",
                                  ch,
                                  Array.Exists(str,
                                               s => {
                                                   if (String.IsNullOrEmpty(s))
                                                       return false;

                                                   if (s.Substring(0, 1).ToUpper() == ch.ToString())
                                                       return true;
                                                   else
                                                       return false;
                                               }));
        }
    }
}


/*
run:
 
One or more strings begin with 'C': True
One or more strings begin with 'J': True
One or more strings begin with 'V': True
One or more strings begin with 'P': True
One or more strings begin with 'D': False

*/

 



answered Apr 16, 2016 by avibootz
...