简体   繁体   English

检查字符串的第一个字母是否与 c# 中 char 数组的元素匹配

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

I am a beginner in c#.我是 c# 的初学者。 I am trying to check if the first letter of postal code matches any element of char array.我正在尝试检查邮政编码的第一个字母是否与 char 数组的任何元素匹配。 If it does not match any of the elements of the char array, it returns false.如果它不匹配 char 数组的任何元素,则返回 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.例如,如果邮政编码是 N2L0G6。 first letter will be N. Bool should be true.第一个字母是 N。Bool 应该是真的。 Since first letter is in the char array.由于第一个字母在 char 数组中。

A. Linq 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 B. foreach

With foreach you want to find any match and then you can stop looking.使用foreach您想找到任何匹配项,然后您就可以停止查找。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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