-3

Please, can you help me please. I have complete select adress from DB but this adress contains adress and house number but i need separately adress and house number.

I created two list for this distribution.

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);
                    }
                }
user2813962
  • 3
  • 1
  • 1
  • 4
  • Please cleanup and format your sample so it is clear what is expected. Also provide input and expected output values, remove SQL code as unrelated... In general check http://www.sscce.org for writing good sample code for question. – Alexei Levenkov Oct 16 '13 at 16:45

4 Answers4

0

Use .Split on your string that results. Then you can index into the result and get the parts of your string.

var parts = s.Split(' ');
// you can get parts[0] etc to access each part;
Rob G
  • 3,496
  • 1
  • 20
  • 29
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...
}
Yosi Dahari
  • 6,794
  • 5
  • 24
  • 44
  • can you implement this code to my example. Please. I need watch it in direct examples :¨) – user2813962 Oct 16 '13 at 16:49
  • ok so this code: string s = reader_org.GetString(0); // Return me example Karlínka over 5387/26 But i need this return value separately to three value. Adress House number OR is index of char (/) After this char (/) value must will be return to my declared string oc. But code must control if house number is really house number and oc is oc because now house number return me example after one space "Over 517/13" – user2813962 Oct 16 '13 at 17:01
  • @AlexeiLevenkov commented and said some useful things, once your post will be more specific, so can the answers. – Yosi Dahari Oct 16 '13 at 17:05
0

Here is a way to split it the address into House Number and Address without regex and only using the functions of the String class.

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
Cubicle.Jockey
  • 3,288
  • 1
  • 19
  • 31
0

You might find the street name consists of multiple words, or the number appears before the street name. Also potentially some houses might not have a number. Here's a way of dealing with all that.

//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);
    }
}
Weyland Yutani
  • 4,682
  • 1
  • 22
  • 28
  • Can you tell me references for function Match? – user2813962 Oct 16 '13 at 17:04
  • the Regex class is in System.Text.RegularExpressions and is useful for matching things in strings – Weyland Yutani Oct 16 '13 at 17:07
  • If your string is predictable I would go with one of the other answers that uses String.Split. But if the addresses can have different formats it's might be worth using a regular expression – Weyland Yutani Oct 16 '13 at 17:09
  • I tested your scripts and it is really nice, can you edited this scripts for two value? Bacause now its returned me first int value what found. But if adress contaions (/) and after this char is next value so i need separately number before / and after / value if adress contaions char(/). Do you understand me? – user2813962 Oct 16 '13 at 17:13
  • i have updated. what if the address number contains a letter? eg 72/2a – Weyland Yutani Oct 16 '13 at 17:32
  • really thx for help it works now i used this, check my first post. – user2813962 Oct 16 '13 at 17:57