美文网首页
限制字符串的长度不大于指定值

限制字符串的长度不大于指定值

作者: 全新的饭 | 来源:发表于2021-12-28 09:52 被阅读0次
    private string RestrictInputContentLength(string value, int length = 16)
    {
        string newValue = string.Empty;
        int newLength = 0;
        foreach (var v in value)
        {
            int l = System.Text.Encoding.UTF8.GetBytes(v.ToString()).Length;
            l = Mathf.Min(l, 2);
            if (newLength + l <= length)
            {
                newValue += v;
                newLength += l;
            }
            else
            {
                break;
            }
        }
        return newValue;
    }
    

    相关文章

      网友评论

          本文标题:限制字符串的长度不大于指定值

          本文链接:https://www.haomeiwen.com/subject/csfpqrtx.html