c# 中一行文本中的单词比较

问题描述 投票:0回答:2

嗨,我在我的项目中使用 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,and,a,bird,这是正确的,但我想要的是如下所示的东西

has - 存在于第一个字符串中,不存在于第二个字符串中

还有一只鸟 - 不存在于第一个字符串中,但存在于第二个字符串中

例如,第二个用户案例

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

这里输出应该是

添加 - 存在于第一个字符串中,但不存在于第二个字符串中

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

我想要一些指示,这些指示出现在第一个而不是第二个中,出现在第二个而不是第一个中,并且比较应该逐字而不是逐个字符。有人可以帮我解决这个问题吗?任何帮助,将不胜感激。谢谢

c# .net asp.net-core string-comparison
2个回答
0
投票
str2Words.Except(str1Words)

查找

str2Words
中不在
str1Words
中的单词。

str1Words.Except(str2Words)

查找

str1Words
中不在
str2Words
中的单词。

由于您分别需要这两者,因此您需要避免将它们连接起来,而是对它们中的每一个使用 Join 以获得空格分隔的结果并附加您为它们计划的“当前”附录。


0
投票

我建议匹配单词

令 word 为字母和撇号的序列

正则表达式的帮助下,然后查询两个给定字符串的匹配:

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)
(meet's, 3)
(a, 3)
(dog, 3)
(has, 1)
(and, 2)
(bird, 2)

小提琴

© www.soinside.com 2019 - 2024. All rights reserved.