简体   繁体   English

如何在C#中比较2个字符串

[英]how to compare 2 strings in c#

I have some string like this: str1 = STA001, str2 = STA002, str3 = STA003 and have code to compare strings: 我有一些这样的字符串:str1 = STA001,str2 = STA002,str3 = STA003,并具有比较字符串的代码:

private bool IsSubstring(string strChild, string strParent)
    {
            if (!strParent.Contains(strChild))
            {
                return false;
            }
            else return true;
    }

If I have strChild = STA001STA002 and strParent = STA001STA002STA003 then return true but when I enter strChild = STA001STA003 and check with strParent = STA001STA002STA003 then return false although STA001STA003 have contains in strParent. 如果我有strChild = STA001STA002和strParent = STA001STA002STA003,则返回true,但是当我输入strChild = STA001STA003并检查strParent = STA001STA002STA003时,则返回false,尽管STA001STA003中包含strParent。 How can i resolve it? 我该如何解决?

What you're describing is not a substring . 您所描述的不是子字符串 It is basically asking of two collections the question "is this one a subset of the other one?" 基本上是问两个集合的问题:“这是另一个集合的子集吗?” This question is far easier to ask when the collection is a set such as HashSet<T> than when the collection is a big concatenated string. 这个问题要容易得多问什么时候该集合是一组HashSet<T>比当收集是一个很大的连接字符串。

This would be a much better way to write your code: 这将是编写代码的更好方法:

var setOne = new HashSet<string> { "STA001", "STA003" };

var setTwo = new HashSet<string> { "STA001", "STA002", "STA003" };

Console.WriteLine(setOne.IsSubsetOf(setTwo)); // True
Console.WriteLine(setTwo.IsSubsetOf(setOne)); // False

Or, if the STA00 part was just filler to make it make sense in the context of strings, then use int s directly: 或者,如果STA00部分只是填充STA00以使其在字符串上下文中有意义,则直接使用int

var setOne = new HashSet<int> { 1, 3 };

var setTwo = new HashSet<int> { 1, 2, 3 };

Console.WriteLine(setOne.IsSubsetOf(setTwo)); // True
Console.WriteLine(setTwo.IsSubsetOf(setOne)); // False

The Contains method only looks for an exact match, it doesn't look for parts of the string. Contains方法仅查找完全匹配,而不查找字符串的一部分。

Divide the child string into its parts, and look for each part in the parent string: 将子字符串分成几部分,然后在父字符串中查找每个部分:

private bool IsSubstring(string child, string parent) {
  for (int i = 0; i < child.Length; i+= 6) {
    if (!parent.Contains(child.Substring(i, 6))) {
      return false;
    }
  }
  return true;
}

You should however consider it cross-part matches are possible, and if that is an issue. 但是,您应该考虑跨部分匹配是可能的,如果这是一个问题。 Eg looking for "1STA00" in "STA001STA002" . 例如在"1STA00"中寻找"STA001STA002" If that would be a problem, then you should divide the parent string similarly, and only make direct comparisons between the parts, not using the Contains method. 如果那是一个问题,那么您应该类似地划分父字符串,并且仅在部分之间进行直接比较,而不使用Contains方法。

Note: Using hungarian notation for the data type of variables is not encouraged in C#. 注意:在C#中,不建议对变量的数据类型使用匈牙利表示法。

This may be a bit on the overkill side, but it might be incredibly beneficial. 从矫kill过正的角度来看这可能有点,但它可能会带来难以置信的好处。

private static bool ContainedWord(string input, string phrase)
{
     var pattern = String.Format(@"\b({0})", phrase);
     var result = Regex.Match(input, pattern);

     if(string.Compare(result, phrase) == 0)
          return true;

     return false;
}

If the expression finds a match, then compare the result to your phrase. 如果表达式找到匹配项,则将结果与您的短语进行比较。 If they're zero, it matched. 如果它们为零,则匹配。 I may be misunderstanding your intent. 我可能会误解你的意图。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM