关闭

截取字符串函数( 截取左边N个字符 )

682人阅读 评论(0) 收藏 举报

//string QYNameAndID = "61.164.228.8:24F74782733CBB7DF98D2111:3300035082302:浙江汇信科技有限公司测试KEY-50号";
        string name = QYNameAndID.Substring(QYNameAndID.LastIndexOf(":"));
        string qyName = name.Remove(0, 1);
        // 获取企业工商注册号
        string idNO = CutStr(QYNameAndID, QYNameAndID.LastIndexOf(":"));
        string ID = idNO.Substring(idNO.LastIndexOf(":"));
        string qyID = idNO.Remove(0, 1);

 #region 截取字符串函数( 截取左边N个字符 )
    public string CutStr(string sInString, int iCutLength)
    {
        if (sInString == null || sInString.Length == 0 || iCutLength <= 0)
            return "";
        int iCount = System.Text.Encoding.GetEncoding("Shift_JIS").GetByteCount(sInString);
        if (iCount > iCutLength)
        {
            int iLength = 0;
            for (int i = 0; i < sInString.Length; i++)
            {
                int iCharLength = System.Text.Encoding.GetEncoding("Shift_JIS").GetByteCount(new char[] { sInString[i] });
                iLength += iCharLength;
                if (iLength == iCutLength)
                {
                    sInString = sInString.Substring(0, i + 1);
                    break;
                }
                else if (iLength > iCutLength)
                {
                    sInString = sInString.Substring(0, i);
                    break;
                }
            }
        }
        return sInString;
    }
    #endregion 

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:112670次
    • 积分:1718
    • 等级:
    • 排名:千里之外
    • 原创:56篇
    • 转载:7篇
    • 译文:0篇
    • 评论:34条
    最新评论