简体   繁体   English

如何比较字符串中的每个单词-UWP

[英]How to compare each words of string with another - uwp

I want to take each word from a string and need to check if it is same as another string. 我想从一个字符串中提取每个单词,并需要检查它是否与另一个字符串相同。 Order of words may not same for two strings. 两个字符串的单词顺序可能不同。 But it should return true results. 但是它应该返回真实的结果。

For example : 例如 :

          StringtoCompareWith="hi you should do this";
          InputString1="you hi should this do";
          InputString2="you hi ";

when compare "InputString1" with "StringtoCompareWith" it should return a true result. 当将“ InputString1”与“ StringtoCompareWith”进行比较时,它应返回真实结果。 And if it is with "InputString2" it will return false. 如果使用“ InputString2”,它将返回false。 How can I achieve this in a fastest way ? 如何以最快的方式实现这一目标?

Split both strings and use Except , but also check that both contains the same number of words. 拆分两个字符串并使用Except ,但还要检查它们是否包含相同数量的单词。

var StringtoCompareWith = "hi you should do this";
var InputString1 = "you hi should this do";

var first = StringtoCompareWith.Split();
var second = InputString1.Split();

bool isEqual = first.Length == second.Length && !first.Except(second).Any();

Same answer, but using foreach loop. 相同的答案,但是使用foreach循环。 Here you can split sentence into words and check if one of the two array contains each word. 在这里,您可以将句子拆分为单词,并检查两个数组之一是否包含每个单词。

Here is the code: 这是代码:

 using System;

public class Program
{
    public static void Main()
    {
        string StringtoCompareWith="hi you should do this";
        string InputString1="you hi should this do";
        string InputString2="you hi ";

        bool isEqual = CheckString(StringtoCompareWith, InputString1);
        //bool isEqual = CheckString(StringtoCompareWith, InputString2);

        Console.WriteLine(isEqual ? "Both are equals" : "Not equal");

    }
    public static bool CheckString(string inp1, string inp2){
        string[] split1 = inp1.Split(' ');
        string[] split2 = inp2.Split(' ');

        //Worst case: when lenght are not same
        if(split1.Length != split2.Length)
            return false;
        else{
            //Time complexity: O(n) 
            foreach(string s1 in split1){
                if(!inp1.Contains(s1))
                    return false;
            }
        }
    return true;
    }
}

Here is the link: dotNetFiddler 这里是链接: dotNetFiddler

Split your string on Space, keep only the not null value then check if all of them are in the reference array with SequenceEqual . 在Space上分割字符串,仅保留非null值,然后使用SequenceEqual检查所有它们是否都在引用数组中。 You can also clear duplicate from the list if you need to. 如果需要,您还可以从列表中清除重复项。

string StringtoCompareWith = "hi you should do this";
string InputString1 = "you hi should this do";
string InputString2 = "you hi ";

var result = StringtoCompareWith.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries).OrderBy(x => x)
                        .SequenceEqual(InputString1.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries).OrderBy(x => x));

Here's a way to do this- 这是执行此操作的方法-

string StringtoCompareWith = "hi you should do this";
string InputString1 = "you hi should this do";
string InputString2 = "you hi";

var arr1 = StringtoCompareWith.Split(' ');
var arr2 = InputString1.Split(' ');

bool check = arr1.Count() == arr2.Count() ? ((arr1.Intersect(arr2).Count() == arr1.Count()) ? true : false) : false;

So I am comparing StringtoCompareWith to InputString1 which you can change as per your needs. 所以我将StringtoCompareWithInputString1进行比较,您可以根据需要进行更改。

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

相关问题 如何将一个字符串数组的每个元素与另一个字符串数组的每个元素进行比较? - How can I compare each element of one string array to every element of another string array? 如何将字符串与文件中的每个项目进行比较? - How to Compare a string to each item in a file? 比较字符串与更改位置的单词 - Compare string with words changing positions 如何将一个字符串与另一个字符串进行比较 - How do I compare one string with another 如何确定一个字符串是否包含来自另一个字符串的单词 - How to determine if one string contains words from another string 如何获取字符串中单词之间的每个数字作为要替换的单独值 - How to get each number between words in string as separate value to replace 如何在UWP的ListView项中突出显示某些单词? - How to highlight certain words in a ListView item on UWP? 如何对 uwp 中的每个类别进行计数 - How to count for each category in uwp 如何使用c#比较字符串与另一个字符串 - How to compare string with another string using c# 找出字符串列表是否包含来自另一个字符串的单词排列(每个组合的计数器) - Find out if a list of strings contains permutations of words from another string (counter for each combination)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM