简体   繁体   English

SQL CLR C#用户定义的函数-从地址获取房屋或公寓编号

[英]SQL CLR C# User Defined Function - Get house or flat number from address

I have the following SQL CLR C# UDF: 我有以下SQL CLR C#UDF:

using System;
using System.Data;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using Microsoft.SqlServer.Server;
using System.Collections;
using System.Text;

public partial class UserDefinedFunctions
{
    [Microsoft.SqlServer.Server.SqlFunction]
    public static SqlString clrFn_GetDigits(string theWord)
    {

        if (theWord == null) { theWord = ""; }
        string newWord = "";

        char[] KeepArray = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '\\', '/', '-', ' '};


        foreach (char thischar in theWord)
        {
            foreach (char keepchar in KeepArray)
            {
                if (keepchar == thischar)
                {     
                    newWord += thischar;
                }    
            }  
        }

        return (SqlString)(newWord.Trim());
    }
}

This works great so far except for addresses like the below: 到目前为止,除以下类似地址外,此方法都有效:

141A, Some Street Avenue
4b, St Georges Street
16E Test Avenue

I want my function to return 141A, 4b and 16E 我希望我的函数返回141A,4b和16E

Any ideas? 有任何想法吗?

I have not tested the below code, but something along those lines, some error checking will need to be in place to ensure the converts do not fail but this solution gives you what you need 我没有测试下面的代码,但是根据这些内容,需要进行一些错误检查以确保转换不会失败,但是此解决方案可以为您提供所需的东西

    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 {

                }
            }

        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM