大家来发现截取字符串的问题

方法如下:

public static string LeftB(string var, int length)
        {
            if (var == null || "" == var || 0 >= length)
            {
                return "";
            }
            System.Text.Encoding enc = System.Text.Encoding.GetEncoding("Shift_JIS");
            byte[] bytes = enc.GetBytes(var);
            if (length > bytes.Length)
            {
                return var;
            }
            else
            {
                string ret = enc.GetString(bytes, 0, length);
                if (true == var.StartsWith(ret))
                {
                    return ret;
                }
                else
                {
                    return ret.Substring(0, ret.Length - 1);
                }
            }
        } 

你可能感兴趣的:(string,byte,null)