A few months back I created several SQL CLR C# functions for a data cleaning project that I was working on. I've had to revisit the code due to a number of bugs that have been found recently.
One of the bugs is the way my C# code strips house numbers from addresses.
The function that I use is below. It works really well for most cases, for example:
Hans-böckler-ring 23A I get 23A
but Hans-böckler-ring 23 A I get 23
How do I make it so that if there is a space and the next character is not a word then pull the number and the chacater? Any ideas?
public static string GetDigits(string theWord) { if (String.IsNullOrEmpty(theWord)) { theWord = ""; } string newWord = ""; char[] KeepArray = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '\\', '/', '-', ' ' };
foreach (char thischar in theWord) { if (KeepArray.Contains(thischar)) { newWord += thischar; } else if (Char.IsLetter(thischar) && newWord.Length > 0) { try { if (Char.IsDigit((Convert.ToChar(newWord.Substring(newWord.Length - 1, 1))))) { newWord += thischar; } } catch { } } } newWord = newWord.Replace(" ", " |").Replace("| ", "").Replace("|", "");
return newWord.Trim(); }
www.SQL4n00bs.com