【问题标题】:Extract address fraction for later use in a C# class提取地址分数以供以后在 C# 类中使用
【发布时间】:2014-11-07 15:25:21
【问题描述】:

我需要查看我的地址是否包含某些部分,但不是全部。像 Hwy 1/158 这样的地址不应匹配。现在我只匹配 1/4、1/2、3/4。稍后我可能会根据数据添加到列表中,但目前这将解决我的绝大多数问题。

目前我只是像这样测试“1/2”

if (Address.Contains("1/2"))
{
    Address = Address.Replace("1/2", "");
    Fraction = "1/2";
}

如何匹配多个分数,删除这些分数,保存分数匹配以供以后分配给分数?

【问题讨论】:

  • 列出要替换的分数,搜索每个分数,找到后替换/保存。

标签: c# replace match street-address


【解决方案1】:

您可以使用Regex.Replace 方法匹配多个模式并使用其求值器保存匹配的元素:

var fractions = new[] { "1/2", "1/4", "3/4" };
var address = "Street 1/4";
List<string> matched = new List<string>();
string replaced = Regex.Replace(address, string.Join("|", fractions), match =>
{
    matched.Add(match.Value);;
    return string.Empty;
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-09-10
    • 2014-10-19
    • 2021-10-06
    • 2015-12-31
    • 2017-04-19
    • 1970-01-01
    • 1970-01-01
    • 2016-10-19
    相关资源
    最近更新 更多