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

  • 相关阅读:
    转贴ARM NEON 优化的例子
    GP(General-purpose Processor)与DSP的存储器结构区别
    arm中的饱和指令
    MIPS,MCPS, MHz for voice codec
    免费提供万方论文
    ARM CORTEX Ax NEON 中的加法指令
    android C编程技巧 及 C/C++开发测试(转)
    SQL Server 存储过程的经典分页 GO
    详细设计说明书大纲 GO
    正则表达式介绍 GO
  • 原文地址:https://www.cnblogs.com/kuair/p/4157799.html
Copyright © 2011-2022 走看看