[英]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. 所以我将StringtoCompareWith
与InputString1
进行比较,您可以根据需要进行更改。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.