簡體   English   中英

C#限制字符串長度

[英]C# limit length of a string

我有以下課程:

public class VendorClass {         
    public int VendorID { get; set; }
    public string VendorName { get; set; }
}

上面的字段匹配數據庫表中的字段。 在說VendorName的情況下,我如何給它一個字段寬度?

VendorName映射到數據庫中的一個字段varchar(15)

您不能限制字符串的長度,但可以使用帶有支持字段的屬性來實現所需的結果:

public class VendorClass
{
    public int VendorID {  get; set; }

    private string _vendorName;

    public string VendorName
    {
        get { return _vendorName; }
        set
        {
            if (value.Length > 15)
            {
                _vendorName = value.Substring(0,15);                    
            } else { 
                _vendorName = value;
            }
        }
    }
}

C# 中的字符串幾乎具有任意長度。

從數據庫加載時,它會自動適應實際的字符串長度。 保存到數據庫時,您的業務邏輯、數據層或 ORM(視情況而定)將需要確保正確的最大長度。

字符串在 C# 中不能有固定長度。 您將不得不通過其他一些機制(如驗證)來處理 db 長度。 沒有更多細節,真的無法告訴你更多。

我剛剛遇到了你所描述的問題,並找到了一種創建有限長度字符串的方法。 當數據庫中只有有限的 varchar 長度定義時,可能有點不靈活但簡潔。

首先介紹一些基礎類:

public class Length16
{
    public static int StringLength { get => 16; }
}

public class Length8
{
    public static int StringLength { get => 8; }
}

public class Length15
{
    public static int StringLength { get => 15; }
}

public class LimitedLengthString<T>
{
    private string _sValue;

    public LimitedLengthString(string sNewValue)
    {
        _sValue = sNewValue;
    }

    public static implicit operator LimitedLengthString<T>(string sNewValue)
    {
        var prop = typeof(T).GetProperty("StringLength");

        int iLength = (int)prop.GetValue(null);

        if (sNewValue.Length > iLength)
        {
            throw new Exception($"New string is too long! Allowed length {iLength}.");
        }

        return new LimitedLengthString<T>(sNewValue);
    }

    public static implicit operator string(LimitedLengthString<T> sSource)
    {
        return sSource.ToString();
    }

    public override string ToString()
    {
        return _sValue; 
    }
}

public class AutoTruncatedString<T>
{
    private string _sValue;

    public AutoTruncatedString(string sNewValue)
    {
        _sValue = sNewValue;
    }

    public static implicit operator AutoTruncatedString<T>(string sNewValue)
    {
        var prop = typeof(T).GetProperty("StringLength");

        int iLength = (int)prop.GetValue(null);

        return new AutoTruncatedString<T>(sNewValue.Substring(0, iLength));
    }

    public static implicit operator string(AutoTruncatedString<T> sSource)
    {
        return sSource.ToString();
    }

    public override string ToString()
    {
        return _sValue;
    }
}

像這樣使用它們:

LimitedLengthString<Length8> sLimitedLength8;
sLimitedLength8 = "asdfgasdfg"; // will error out

AutoTruncatedString<Length8> sAutoTruncated8;
sAutoTruncated8 = "asdfgasdfg"; // will be truncated

如果您嘗試分配長度超過 8 的字符串,sLimitedLength8 將引發錯誤,而 sAutoTruncated8 將截斷您分配給它的字符串。

對您而言,您可以通過以下方式定義 VendorName:

public LimitedLengthString<Length15> VendorName { get; set; }

希望這可以幫助你。

我會質疑為什么你會在 c# 代碼中這樣做。 但是, 此鏈接有幾種解決方法。 我認為截斷或采用 subsring 是最好的選擇。 您還可以確保 UI(或模型視圖)處理諸如此類的細節。

我不確定你在問什么,但如果你想知道字符串的最大長度,這個問題可以幫助你。

如果您想限制輸入的字符數,我建議您使用服務器端驗證和/或客戶端驗證。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM