Matching 2 strings in C#

徘徊边缘 提交于 2019-12-12 06:39:52

问题


I have 2 strings. These 2 strings can differ in size. I want to look at these 2 strings finding matching sequences. Once I find a change I want to print that word in Capital and then continue on in my string until I find another change and so on. I'm not sure how I would go about this I tried looking at words as a whole but I'm having issues with that. Basically I will have 2 string something like this string one="This is a new value" and string two= "This This is a new also brand value". I want go though each string from the start and find the matching sequences e.g. "This is" stop at string realise it has changed as string was added change it to upper case and then carry on. Expected output ="THIS this is a new ALSO BRAND value "

Some code I was trying. I don't think this is the right approach.

 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();
        }

回答1:


Is this what you're looking for? The description isn't fantastic, but judging by the answers this seems to be in the same ballpark, and it uses LINQ for less code and complication.

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();
    }
}

You can break this out to a separate method and pass your variables in as parameters.




回答2:


You can convert string to char array and compare chars one by one. You can use the following code i guess.

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"



回答3:


Is that what you need? (Updated)

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();

Note that if for value1="This is a new Value" and value2="This is also a new value" output should be "This is ALSO a new value" than for value1="This is text one" and value2="This is string text two not string one" output will be "This is STRING text TWO NOT STRING one", not "This is STRING TEXT TWO NOT STRING ONE" as you mentioned before.



来源:https://stackoverflow.com/questions/42210366/matching-2-strings-in-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!