嗨,我在我的项目中使用C#语言,我正试图获得类似下面的输出.

 string str1 = "Cat meet's a dog has";
 string str2 = "Cat meet's a dog and a bird";

 string[] str1Words = str1.ToLower().Split(' ');
 string[] str2Words = str2.ToLower().Split(' ');

 var uniqueWords = str2Words
   .Except(str1Words)
   .Concat(str1Words.Except(str2Words))
   .ToList();

这给了我一只正确的鸟,但我想要的是下面这样的东西

第一个字符串中的Has-Present不存在于第二个字符串中

和一只鸟--不是出现在第一串中,而是出现在第二串中

例如,第二个用例

String S1 = "Added"
String S2 = "Edited"

这里的输出应该是

已添加-出现在第一个字符串中,而不出现在第二个字符串中

已编辑-不在第一个字符串中出现,但在第二个字符串中出现

我希望有一些指示是在第一个而不是第二个,在第二个而不是第一个,比较应该是逐字而不是逐字.有人能帮帮我吗?任何帮助都将不胜感激.谢谢

推荐答案

我建议搭配词语

让单词由字母和撇号组成的序列

regular expression的帮助下(请注意,拆分不考虑标点符号,因此catcat,cat!将被视为三个不同的单词),然后查询两个给定字符串的匹配:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions; 

...

private static readonly Regex WordsRegex = new Regex(@"[\p{L}']+"); 

// 1 - in text1, 2 - in text2, 3 - in both text1 and text2 
private static List<(string word, int presentAt)> MyWords(string text1, string text2) {
  HashSet<string> words1 = WordsRegex
    .Matches(text1)
    .Cast<Match>()
    .Select(match => match.Value)
    .ToHashSet(StringComparer.OrdinalIgnoreCase);

  HashSet<string> words2 = WordsRegex
    .Matches(text2)
    .Cast<Match>()
    .Select(match => match.Value)
    .ToHashSet(StringComparer.OrdinalIgnoreCase);

  return words1
    .Union(words2)
    .Select(word => (word, presentAt: (words1.Contains(word) ? 1 : 0) | 
                                      (words2.Contains(word) ? 2 : 0)))
    .ToList();
}

演示:

string str1 = "Cat meet's a dog has";
string str2 = "Cat meet's a dog and a bird";
    
var result = MyWords(str1, str2);
    
var report = string.Join(Environment.NewLine, result);
    
Console.Write(report);

输出:

(Cat, 3)         # 3: in both str1 and str2 
(meet's, 3)      # 3: in both str1 and str2
(a, 3)           # 3: in both str1 and str2
(dog, 3)         # 3: in both str1 and str2 
(has, 1)         # 1: in str1 only
(and, 2)         # 2: in str2 only
(bird, 2)        # 2: in str2 only 

Fiddle

如果你想要一个冗长的输出:

string str1 = "Cat meet's a dog has";
string str2 = "Cat meet's a dog and a bird";
    
string[] options = new string[] {
  "not present",
  "present in first string not present in second string",
  "not present in first string but present in second string",
  "present in first string and present in second string"
};
        
var report = string.Join(Environment.NewLine, result
  .Select(pair => $"{pair.word} - {options[pair.presentAt]}"));

Console.Write(report);

输出:

Cat - present in first string and present in second string
meet's - present in first string and present in second string
a - present in first string and present in second string
dog - present in first string and present in second string
has - present in first string not present in second string
and - not present in first string but present in second string
bird - not present in first string but present in second string

Csharp相关问答推荐

将用户转移到Azure AD B2C中的公司

Mstest + Coverlet在收件箱中没有达到100%的覆盖率,但我在Codecov中得到了100%的覆盖率

从Key Vault中提取值以在本地dev web.connect中使用

Autofac:如何防止丢弃通过ServicCollection注册的服务?

在命令行中使用时安装,但在单击时不会安装

哪个nuget包含SecurityStampValidatorOptions

如何测量在使用UTF8而不是C#中的UTF16编码字符串时内存使用量的增长

在命名管道上使用GRPC ASP.NET核心时如何配置命名管道权限

在实体框架中处理通用实体&S变更跟踪器

如何使用MailKit删除邮箱?

为什么AggregateException的Catch块不足以处理取消?

JsonSchema.Net删除假阳性判断结果

C#LINQ延迟执行和嵌套方法

.NET8Blazor-为什么Rapzor渲染在for循环之后显示?

类/值和日期的泛型方法

我可以强制System.Text.Json.JsonSerializer以非递归方式工作吗?

如何使用.NET Aspire从Blazor应用程序与GRPC API通信?

我应该为C#12中的主构造函数参数创建私有属性吗?

try 创建一个C#程序,该程序使用自动实现的属性、覆盖ToString()并使用子类

为什么我的属性即使没有显式地设置任何[必需]属性,也会显示验证?