在C#中提取给定字符串中的所有想要的单词

时间:2018-03-15 09:27:19

标签: c#

如何在字符串中提取所有想要的单词并在C#中计算?

这是示例

这些是我想要提取的词:一,二,三

这是给定的字符串:一次两加一等于三。

结果应该显示一个二三一和四

非常感谢提前

3 个答案:

答案 0 :(得分:1)

它相当容易。您可以采用多种方式,但我选择了splitLinq

String.Split Method

  

返回包含此实例中的子字符串的字符串数组   由指定字符串或Unicode的元素分隔   字符数组。

Enumerable.Select Method (IEnumerable, Func)

  

将序列的每个元素投影到新表单中。

var myList = new List<string>
                  {
                     "one",
                     "two",
                     "three"
                  };

var input = "One times two plus one equals three";

var inputList = input.Split(new []{' '},StringSplitOptions.RemoveEmptyEntries)
            .Select(x => x.ToLower());

var result = inputList.Where(x => myList.Contains(x.ToLower()))
            .ToList();

Console.WriteLine(string.Join(", ", result));
Console.WriteLine(result.Count());

See the demo here

更新了mjwills comoment

  

那会匹配一个吗?

确保其真正区分大小写

myList.Contains(x.ToLower())

更新2

或者mjwills再次指出

  

你甚至可以考虑使用不区分大小写的HashSet来制作   包含更快 - 无需使用ToLower

var set = new HashSet<string>(StringComparer.OrdinalIgnoreCase)
         {
            "one",
            "two",
            "three"
         };
var input = "One times two plus one equals three";
var inputList = input.Split(new []{' '},StringSplitOptions.RemoveEmptyEntries).Select(x => x.ToLower());
var result = inputList.Where(x => set.Contains(x)).ToList();
Console.WriteLine(string.Join(", ", result));
Console.WriteLine(result.Count());

答案 1 :(得分:0)

我不认为你想要的就是你要求但这里是我的代码:

public static int ExtractWordsOutOfString(this string s, List<string> filter)
        {
            int ret = 0;

            string[] s1 = s.Split(' ');

            foreach (string eachWord in s1)
            {
                foreach (string eachFilter in filter)
                {
                    if (eachWord == eachFilter)
                        ret++;
                }
            }


            return ret;
        }

你可以像以下一样使用它:

string k = "one times two plus one equals three";

List<string> localfilter = new List<string>();
localfilter.Add("one");
localfilter.Add("two");
localfilter.Add("three");
localfilter.Add("four");

Console.WriteLine(k.ExtractWordsOutOfString(localfilter));

答案 2 :(得分:-2)

<强> See the demo here

获得所要求的最简单方法是使用拆分功能将输入字符串拆分为一系列单词

请注意,在最后一个单词中,除法函数返回“三个”,最后一个点。

出于这个原因,我创建了unwantedchars数组,你可以在比较中指定你想要忽略的一些字符。

string input = "One times two plus one equals three.";
string[] wanted = { "one", "two", "three" };
string[] words = input.Split(' ');
char[] unwantedChars = { '.' };
var found = words.Where(word =>
{
    foreach (var wantedWord in wanted)
    {
        string trimmedWord = word.Trim(unwantedChars);
        if (wantedWord.Equals(trimmedWord, StringComparison.InvariantCultureIgnoreCase))
            return true;
    }
    return false;
});
Console.WriteLine(string.Join(", ", found));
Console.WriteLine($"Count: {found.Count()}");

你也可以把它变成一个函数并像这样使用它

static void Main(string[] args)
{
    string input = "One times two plus one equals three.";
    string[] wanted = { "one", "two", "three" };
    int count = CountWords(input, wanted, out var found);
    Console.WriteLine(string.Join(", ", found));
    Console.WriteLine($"Count: {found.Count()}");
}

static int CountWords(string input, IEnumerable<string> wantedWords, out IEnumerable<string> found)
{
    string[] words = input.Split(' ');
    char[] unwantedChars = { '.' };
    found = words.Where(word =>
    {
        foreach (var wantedWord in wantedWords)
        {
            string trimmedWord = word.Trim(unwantedChars);
            if (wantedWord.Equals(trimmedWord, StringComparison.InvariantCultureIgnoreCase))
                return true;
        }
        return false;
    });
    return found.Count();
}

and here's the result!