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

  • 相关阅读:
    2017微软骇客马拉松精彩大回Fun:不一样的Hacker,一Young的Cool
    老板这种生物:只看结果,不问过程
    小目标 | 分解任务,聪明人只设达得到的“小目标”
    本号讯 | 微软被 Forrester 评为销售服务自动化解决方案领导者
    IT圈网红,抢鲜围观
    云时代“非诚勿扰”
    安装conda后去除终端出现的(base)字样
    Ubuntu18.04 安装 Anaconda3
    高斯模糊
    准确率(Accuracy) 精确率(Precision) 与 召回率(Recall)
  • 原文地址:https://www.cnblogs.com/kuair/p/4157799.html
Copyright © 2011-2022 走看看