我如何检查两个字符串是否包含相同的字母

时间:2015-03-22 11:48:04

标签: c# regex

我目前正在C#中完成编程挑战,我被困在主要部分。应用程序必须使用两个单词并查看它们是否包含相同的字母。如何查看input1input2是否包含相同的字母?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Text.RegularExpressions;

namespace Words_With_Enemies
{
    class Program
    {

        static string input1, input2;

        public void findLetters()
        {
            bool regexWord1 = Regex.IsMatch(input1, @"^[a-zA-Z]+$");
        }

        static void Main(string[] args)
        {

            Console.WriteLine("Please enter two words");
            input1 = Console.ReadLine();
            input2 = Console.ReadLine();

            Console.WriteLine("You have entered the following two words:");
            Console.WriteLine(input1);
            Console.WriteLine(input2);

            Console.ReadLine();

        }

     }
}

2 个答案:

答案 0 :(得分:5)

如果您想要找到 ,如果两个字符串中的所有字母都相同 ,那么您可以使用System.Linq命名空间中的Except()

bool result = input1.Except(input2).Any();

如果它们不包含相同的字母,它将返回true

此类输入的输出将如下:

  

苹果,Apple =>真
     苹果,香蕉=>真
      Apple,Alep =>假
      Apple,Apple =>假

<强> 更新

如果您想要找到 ,如果两个字符串中都包含任何字母 ,那么您可以使用Intersect()

bool result = input1.Intersect(input2).Any();

如果它们包含至少一个相同的字母,它将返回true。 这些输入的输出将是这样的:

此类输入的输出将如下:

  

苹果,Apple =&gt;真实的苹果,香蕉=&gt;真的Apple,Alep =&gt;   真的是Apple,Onion =&gt;假


其他详细信息:
如果您想查找结果 不区分大小写 ,则可以将这两个代码更改为:

bool result = input1.ToLowerInvariant().Except(input2.ToLowerInvariant()).Any();
bool result = input1.ToLowerInvariant().Intersect(input2.ToLowerInvariant()).Any();

答案 1 :(得分:0)

基本上你想检查两个字符串是否是排列。

    static private bool isPermutation(string myString1, string myString2)
    {
        //If the strings are different lengths, they are not
        //permutations.
        if (myString1.Length != myString2.Length) return false;

        //Create an array to count the number of each specific
        //character in the strings.
        int[] characterCount = new int[256];
        int charIndex;

        //Populate the array with default value 0.
        for (int index = 0; index < 256; index++)
        {
            characterCount[index] = 0;
        }

        //Count the number of each character in the first
        //string. Add the count to the array.
        foreach (char myChar in myString1.ToCharArray())
        {
            charIndex = (int)myChar;
            characterCount[charIndex]++;
        }

        //Count the number of each character in the second
        //string. Subtract the count from the array.
        foreach (char myChar in myString2.ToCharArray())
        {
            charIndex = (int)myChar;
            characterCount[charIndex]--;
        }

        //If the strings are permutations, then each character
        //would be added to our character count array and then
        //subtracted. If all values in this array are not 0
        //then the strings are not permutations of each other.
        for (int index = 0; index < 256; index++)
        {
            if (characterCount[index] != 0) return false;
        }

        //The strings are permutations of each other.
        return true;
    }
}
}