字符串替换使用列表<字符串> [英] string replace using a List<string>

查看:156
本文介绍了字符串替换使用列表<字符串>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个单词列表我想忽略像这样的:

I have a List of words I want to ignore like this one :

public List<String> ignoreList = new List<String>()
        {
            "North",
            "South",
            "East",
            "West"
        };

对于一个给定的字符串,比如第14大街北我希望能够删除的北的一部分,所以基本上这将返回一个函数第14大街时调用。

For a given string, say "14th Avenue North" I want to be able to remove the "North" part, so basically a function that would return "14th Avenue " when called.

我觉得像有什么东西我应该能够做LINQ,正则表达式和替换的组合,可我就是想不通。

I feel like there is something I should be able to do with a mix of LINQ, regex and replace, but I just can't figure it out.

大局观,我试着写一个地址匹配算法。我要过滤掉的话,如街,北,大道,等等。之前我用Levenshtein算法来评估的相似性。

The bigger picture is, I'm trying to write an address matching algorithm. I want to filter out words like "Street", "North", "Boulevard", etc. before I use the Levenshtein algorithm to evaluate the similarity.

推荐答案

这个怎么样:

string.Join(" ", text.Split().Where(w => !ignoreList.Contains(w)));

或净3:

string.Join(" ", text.Split().Where(w => !ignoreList.Contains(w)).ToArray());

请注意,此方法分割字符串成单个单词,因此仅删除整个单词。这样,它会正常工作与像地址北安普顿路#123 与string.replace 无法处理。

Note that this method splits the string up into individual words so it only removes whole words. That way it will work properly with addresses like Northampton Way #123 that string.Replace can't handle.

这篇关于字符串替换使用列表&LT;字符串&GT;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆