zoukankan      html  css  js  c++  java
  • 截取字符窜(区分中英文)

       /// <summary>
            /// 截取字符窜
            /// </summary>
            /// <param name="p_SrcString">待截取字符串</param>
            /// <param name="p_Length">截取字符串长度</param>
            /// <returns></returns>
            public static string GetSubString(string p_SrcString, int p_Length)
            {
                string myResult = p_SrcString;
                if (p_Length >= 0)
                {
                    byte[] bsSrcString = System.Text.Encoding.GetEncoding("GB2312").GetBytes(p_SrcString);
                    if (bsSrcString.Length > p_Length)
                    {
                        int nRealLength = p_Length;
                        int[] anResultFlag = new int[p_Length];
                        byte[] bsResult = null;
                        int nFlag = 0;
                        for (int i = 0; i < p_Length; i++)
                        {
                            if (bsSrcString[i] > 127)
                            {
                                nFlag++;
                                if (nFlag == 3)
                                    nFlag = 1;
                            }
                            else
                                nFlag = 0;
                            anResultFlag[i] = nFlag;
                        }
                        if ((bsSrcString[p_Length - 1] > 127) && (anResultFlag[p_Length - 1] == 1))
                            nRealLength = p_Length + 1;
                        bsResult = new byte[nRealLength];
                        Array.Copy(bsSrcString, bsResult, nRealLength);
                        myResult = Encoding.GetEncoding("GB2312").GetString(bsResult) + "...";
                    }
                }
                return myResult;
            }

  • 相关阅读:
    HTTPS证书申请相关笔记
    180508
    如何通过 AAR 形式集成 leakcanary-android 服务
    Mysql命令大全
    Python3.x和Python2.x的区别 (转)
    Python学习笔记(二)
    for循环处理列表的显示
    Python学习笔记
    python环境搭建
    Linux下JDK环境的配置
  • 原文地址:https://www.cnblogs.com/houzuofeng/p/3253294.html
Copyright © 2011-2022 走看看