在C#中匹配2个字符串

时间:2017-02-13 17:52:18

标签: c# string-matching

我有2个字符串。这两个字符串的大小可能不同。我想看看这两个找到匹配序列的字符串。一旦我找到了更改,我想在Capital中打印该单词,然后继续使用我的字符串直到找到另一个更改,依此类推。我不确定我会如何解决这个问题我试着把整个单词看成一片但我遇到了问题。基本上我会有2个字符串,比如这个string one="This is a new value"string two= "This This is a new also brand value“。我想从头开始查找匹配的序列,例如”This is“stop at string意识到它已经改变为字符串添加后将其更改为大写然后继续。预期输出= "THIS this is a new ALSO BRAND value "

我正在尝试的一些代码。我不认为这是正确的方法。

 static void Main(string[] args)
        {
            string one = "This is a new value";
            string two = "This This is a new also brand value";

            var coll = two.Split(' ').Select(p => one.Contains(p) ? p : p.ToUpperInvariant());

            Console.WriteLine(string.Join(" ", coll));
            Console.ReadKey();
        }

3 个答案:

答案 0 :(得分:1)

这是你正在寻找的吗?描述并不是很棒,但是从答案来看,这似乎是在同一个球场,并且它使用LINQ来减少代码和复杂性。

class Program
{
    static void Main(string[] args)
    {
        string one = "This is text one";
        string two = "This is string text two not string one";

        var coll = two.Split(' ').Select(p => one.Contains(p) ? p : p.ToUpperInvariant());

        Console.WriteLine(string.Join(" ", coll)); // This is STRING text TWO NOT STRING one
        Console.ReadKey();
    }
}

您可以将其分解为单独的方法,并将变量作为参数传递。

答案 1 :(得分:0)

您可以将字符串转换为char数组并逐个比较字符。您可以使用以下代码。

string one = "this is string one";
        string two = "this is string one or two";


        char[] oneChar = one.ToCharArray();
        char[] twoChar = two.ToCharArray();
        int index = 0;
        List<char> Diff = new List<char>();

        if (oneChar.Length > twoChar.Length)
        {
            foreach (char item in twoChar)
            {
                if (item != oneChar[index])
                    Diff.Add(item);

                index++;
            }

            for (int i = index; i < oneChar.Length; i++)
            {
                Diff.Add(oneChar[i]);
            }
        }
        else if (oneChar.Length < twoChar.Length)
        {
            foreach (char item in oneChar)
            {
                if (item != twoChar[index])
                    Diff.Add(twoChar[index]);

                index++;
            }

            for (int i = index; i < twoChar.Length; i++)
            {
                Diff.Add(twoChar[i]);
            }
        }
        else//equal length
        {
            foreach (char item in twoChar)
            {
                if (item != oneChar[index])
                    Diff.Add(item);
            }
        }

        Console.WriteLine(Diff.ToArray());//" or two"

答案 2 :(得分:0)

这就是你需要的吗? (更新)

Whatever

请注意,如果var value1 = "This is a new Value"; var value2 = "This is also a new value"; var separators = new[] { " " }; var value1Split = value1.Split(separators, StringSplitOptions.None); var value2Split = value2.Split(separators, StringSplitOptions.None); var result = new List<string>(); var i = 0; var j = 0; while (i < value1Split.Length && j < value2Split.Length) { if (value1Split[i].Equals(value2Split[j], StringComparison.OrdinalIgnoreCase)) { result.Add(value2Split[j]); i++; j++; } else { result.Add(value2Split[j].ToUpper()); j++; } } Console.WriteLine(string.Join(" ", result)); Console.ReadKey(); value1="This is a new Value"输出应该value2="This is also a new value"而不是"This is ALSO a new value"value1="This is text one"输出将是value2="This is string text two not string one",而不是"This is STRING text TWO NOT STRING one" {1}}如前所述。