-3

拜托,你能帮帮我吗?我从 DB 中选择了完整的地址,但这个地址包含地址和门牌号,但我需要单独的地址和门牌号。

我为此发行版创建了两个列表。

while (reader_org.Read())
                {
                    string s = reader_org.GetString(0);
                    string ulice, cp, oc;
                    char mezera = ' ';

                    if (s.Contains(mezera))
                    {
                        Match m = Regex.Match(s, @"(\d+)");
                        string numStr = m.Groups[0].Value; 
                        if (numStr.Length > 0)
                        {
                            s = s.Replace(numStr, "").Trim();
                            int number = Convert.ToInt32(numStr);
                        }
                        Match l = Regex.Match(s, @"(\d+)");
                        string numStr2 = l.Groups[0].Value;
                        if (numStr2.Length > 0)
                        {
                            s = s.Replace(numStr2, "").Trim();
                            int number = Convert.ToInt32(numStr2);
                        }

                        if (s.Contains('/'))
                            s = s.Replace('/', ' ').Trim();

                        MessageBox.Show("Adresa: " + s);
                        MessageBox.Show("CP:" + numStr);
                        MessageBox.Show("OC:" + numStr2);
                    }
                    else
                    {
                        Definitions.Ulice.Add(s);
                    }
                }
4

4 回答 4

0

在你的字符串上使用.Split结果。然后您可以索引结果并获取字符串的各个部分。

var parts = s.Split(' ');
// you can get parts[0] etc to access each part;
于 2013-10-16T16:43:33.273 回答
0
using (SqlDataReader reader_org = select_org.ExecuteReader())
{
    while (reader_org.Read())
    {
        string s = reader_org.GetString(0); // this return me for example Karlínkova 514 but i need separately adress (karlínkova) and house number (514) with help index of or better functions. But now i dont know how can i make it.
        var values = s.Split(' ');
        var address = values.Count > 0 ? values[0]: null;
        var number = values.Count > 1 ? int.Parse(values[1]) : 0;

        //Do what ever you want with address and number here...
}
于 2013-10-16T16:45:06.027 回答
0

这是一种将地址拆分为门牌号和地址的方法,无需正则表达式,仅使用String类的功能。

var fullAddress = "1111 Awesome Point Way NE, WA 98122";
var index = fullAddress.IndexOf(" "); //Gets the first index of space

var houseNumber = fullAddress.Remove(index);
var address = fullAddress.Remove(0, (index + 1));

Console.WriteLine(houseNumber);
Console.WriteLine(address);

Output: 1111
Output: Awesome Point Way NE, WA 98122
于 2013-10-16T16:53:06.163 回答
0

您可能会发现街道名称由多个单词组成,或者数字出现在街道名称之前。也可能有些房屋可能没有编号。这是处理所有这些的方法。

//extract the first number found in the address string, wherever that number is.
Match m = Regex.Match(address, @"((\d+)/?(\d+))");
string numStr = m.Groups[0].Value;

string streetName = address.Replace(numStr, "").Trim();
//if a number was found then convert it to numeric 
//also remove it from the address string, so now the address string only
//contains the street name
if (numStr.Length > 0)
{
    string streetName = address.Replace(numStr, "").Trim();
    if (numStr.Contains('/'))
    {
        int num1 = Convert.ToInt32(m.Groups[2].Value);
        int num2 = Convert.ToInt32(m.Groups[3].Value);
    }
    else
    {
        int number = Convert.ToInt32(numStr);
    }
}
于 2013-10-16T16:57:59.817 回答