[英]Check string if it contain any of string array value, then get the substring
This may be a sub-question to this SO Question . 这可能是本SO问题的一个子问题 。 I want to check the string against an array of string or list.
我想根据字符串或列表数组检查字符串。 Example
例
string address = "1st nice ave 1st floor";
//For now, I'm getting the list from a text file but could move to use an EF
List<string> streetType = File.ReadLines(AppDomain.CurrentDomain.BaseDirectory + @"streetType.csv")
.Where(x => x.Length > 0)
.Select(y => y.ToLowerInvariant())
.ToArray();
the purpose is to strip the extra address details after the avenue, the csv file contains all USPS accepted street type. 目的是在大道之后删除额外的地址详细信息,csv文件包含所有USPS接受的街道类型。
This is what I have now 这就是我现在拥有的
//this only returns boolean value, I got this from the SO above
streetType.Any(testaddress.ToLower().Contains);
//I also have this
Array.Exists<string>(streetType, (Predicate<string>)delegate (string s)
{
return testaddress.IndexOf(s, StringComparison.OrdinalIgnoreCase) > -1;
});
I've been looking for hours how to resolve this then I came across the SO question which is exactly what I also want but I need to get the substring to for stripping. 我一直在寻找几个小时如何解决这个问题,然后我遇到了SO问题,这正是我想要的,但我需要将子字符串用于剥离。
If there's a linq query, that would be awesome. 如果有一个linq查询,那将是非常棒的。 The only way I can think of doing this is with foreach and inner if.
我能想到的唯一方法是使用foreach和inner if。
Example of the array values 数组值的示例
Update: 更新:
Here is my answer, I forgot to mention that the array lookup needs to match the exact string from the address string. 这是我的答案,我忘了提到数组查找需要匹配地址字符串中的确切字符串。 I ended up using regex.
我最终使用正则表达式。 This is the expanded/modified answer of @giladGreen.
这是@giladGreen的扩展/修改答案。
var result = from item in streetTypes
let index = Regex.Match(address.ToLowerInvariant(), @"\b" + item.ToLowerInvariant() + @"\b")
where index.Success == true
select address.ToLowerInvariant().Substring(0, index.Index + item.Length);
Can somebody convert this to lambda expression? 有人可以将此转换为lambda表达式吗? I tried I failed.
我试过失败了。
Thank you all 谢谢你们
Use IndexOf
to understand of item is present in address
and if so to return the string after it: 使用
IndexOf
来理解address
是否存在项目,如果是,则返回后面的字符串:
var result = from item in streetType
let index = address.IndexOf(item)
where index != -1
select address.SubString(0, index);
One way to do this would be to simply Split
each address on the streetType
list, and then take the first item (at index[0]
) from the resulting array: 一种方法是简单地
Split
streetType
列表中的每个地址,然后从结果数组中获取第一个项目(在index[0]
):
addresses = addresses
.Select(address => address.Split(streetTypes.ToArray(), StringSplitOptions.None)[0])
.ToList();
I might be inclined to do something like this: 我可能倾向于做这样的事情:
string[] markers = "ave avenue pkwy".Split();
string address = "1st nice ave 1st floor";
var result = markers
.Select((marker, index) => new
{
markerIndex = index,
addressPosition = address.IndexOf(marker)
})
.FirstOrDefault(x => x.addressPosition != -1);
// returns { markerIndex = 0, addressPosition = 9 }
Then result
is an object that is either null (if the marker is not found) or is an object containing both markerIndex
, which tells you which marker was found first, and addressPosition
which tells you the character at which the marker string was found. 然后
result
是一个null (如果没有找到标记)的对象,或者是一个包含markerIndex
的对象,它告诉你最先找到哪个标记, addressPosition
告诉你找到标记字符串的字符。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.