简体   繁体   中英

check if the first letter of string matches the elements of char array in c#

I am a beginner in c#. I am trying to check if the first letter of postal code matches any element of char array. If it does not match any of the elements of the char array, it returns false. Below is the approach:

string firstLetter= "KLMN";
char[] postalLetters = firstLetter.ToCharArray();
string PostalCode = "N2L0G6";
bool firstPostalLetterMatch = true;

foreach(char ch in firstLetter)
{
    if (PostalCode.Substring(0, 1) != postalLetters.ToString())
    {
       firstPostalLetterMatch  = false;
    }
}

if(firstPostalLetterMatch == false)
{
    Console.WriteLine("Error");
}
else
{
    Console.WriteLine("No Error");
}

for example if postal code is N2L0G6. first letter will be N. Bool should be true. Since first letter is in the char array.

A. Linq

using System.Linq;

...

bool firstPostalLetterMatch = 
     postalLetters.Any( l => l == PostalCode[0] ); // BTW. This is case sensitive

This reverses the question a bit. It says: are there any letters in our collection of good letters that match the first letter of the tested postal code.

B. foreach

With foreach you want to find any match and then you can stop looking.

bool firstPostalLetterMatch = false;
foreach(char ch in postalLetters)
{
   if (PostalCode[0] == ch)
   {
      firstPostalLetterMatch  = true;
      break; // Match found, we no longer have to search
   }
}

You probably want something like this:

bool firstPostalLetterMatch = false;
char postCodeFirstLetter = PostalCode.ToCharArray()[0];
foreach(char ch in firstLetter)
{
    if (postCodeFirstLetter == ch)
    {
       firstPostalLetterMatch = true;
       break;
    }
}

Since you said you're a beginner here's an easy to follow method for achieving what you need.

using System;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            string firstLetter = "KLMN";
            int firstLength = firstLetter.Length;
            int i = 0;
            string PostalCode = "N2L0G6";
            while (i < firstLength)
            {
                if (PostalCode[0].ToString().Contains(firstLetter[i]))
                {
                    Console.WriteLine(firstLetter[i] + " matches first letter of " + PostalCode);
                }
                else
                {
                    Console.WriteLine(firstLetter[i] + " does not match the first letter of " + PostalCode);
                }
                i++;
            }
            Console.Read();

        }
    }
}

This outputs

K does not match the first letter of N2L0G6
L does not match the first letter of N2L0G6
M does not match the first letter of N2L0G6
N matches first letter of N2L0G6

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM