zoukankan      html  css  js  c++  java
  • C#截取字符串的方法


        #region 得到字符串的长度
        /// <summary>
        /// 得到字符串的长度
        /// </summary>
        /// <param name="strData"></param>
        /// <returns></returns>
        public static int GetStrLen(string strData)
        {
            System.Text.Encoding encoder5 = System.Text.Encoding.GetEncoding("GB2312");
            return encoder5.GetByteCount(strData);
        }
        #endregion

        #region 切割字符串
        /// <summary>
        /// 切割字符串
        /// </summary>
        /// <param name="s">需要切割的字符串</param>
        /// <param name="i">需要切割的字符串长度</param>
        /// <param name="smore">切割后字符串后面添加的字符串</param>
        /// <returns></returns>
        public static string SubStr(string s, int i, string smore)
        {
            int intResult = 0;
            int j = 0;
            string s1 = s;
            if (GetStrLen(s) > i)
            {
                foreach (char Char in s)
                {
                    if (intResult < i)
                    {
                        j++;
                        if ((int)Char > 127)
                            intResult = intResult + 2;
                        else
                            intResult++;
                    }
                    else
                        break;
                }
                s1 = s.Substring(0, j);
            }
            else
            {
                return s1;
            }
            string temp = s1 + smore;
            if (GetStrLen(s) <= GetStrLen(temp))//增加这一步的判断,避免刚好的字符被切掉
            {
                return s;
            }
            return temp;
        }
        #endregion

  • 相关阅读:
    速耀达数据库安装(速达二次开发)
    WCF错误:由于目标计算机积极拒绝,无法连接;127.0.0.1:3456
    HTTP 错误 403.14
    速耀达企业ERP试用说明流程
    速耀达ERP手机App开始内测,完全兼容速达5000以上的任何版本
    新版速云达App即将发布内测
    如何解决速达软件出现主键、单号、编码相同的报错
    NTP服务器搭建
    linux操作系统不重启添加raid0步骤
    反向代理负载均衡调度:nginx
  • 原文地址:https://www.cnblogs.com/kuair/p/4157799.html
Copyright © 2011-2022 走看看