zoukankan      html  css  js  c++  java
  • 几种常见的字符串长度截取显示方法

    有从网上找的和自己在用的方法:

    1.CSS法<个人认为几种中比较好的方法,可以减少后台程序服务>

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
    <HTML>
     <HEAD>
      <style>
      .p_Name { PADDING-LEFT: 0px; OVERFLOW: hidden; WIDTH: 90px; WORD-BREAK: break-all; HEIGHT: 40px }
      </style>
     </HEAD>
    
     <BODY>
     <div class="p_Name">sssssssssssssssssssssssssssss<br>ssssssssssssssssssssssssssssssssssssss</div>
     </BODY>
    </HTML>
    2.使用正则区分中英文进行双字节符计算 不错
    public static string GetFirstString(string stringToSub, int length) 
            {
                Regex regex = new Regex("[\u4e00-\u9fa5]+", RegexOptions.Compiled);
                char[] stringChar = stringToSub.ToCharArray();
                StringBuilder sb = new StringBuilder();
                int nLength = 0;
                bool isCut=false;
                for(int i = 0; i < stringChar.Length; i++) 
                {
                    if (regex.IsMatch((stringChar[i]).ToString())) 
                    {
                        sb.Append(stringChar[i]);
                        nLength += 2;
                    }
                    else 
                    {
                        sb.Append(stringChar[i]);
                        nLength = nLength + 1;
                    }
    
                    if (nLength > length)
                    {
                        isCut=true;
                        break;
                    }
                }
                if(isCut)
                    return sb.ToString()+"..";
                else
                    return sb.ToString();
            }
    3.编码字节
    public string GetLimitedStr(string aSrcStr,int aLimitedNum)
            {
                if (aLimitedNum<=0) return aSrcStr;
                //String TmpStr=RemoveHTMLStr(aSrcStr);
                String TmpStr=aSrcStr;
                byte[] TmpStrBytes = System.Text.Encoding.GetEncoding("GB2312").GetBytes(TmpStr);
                if(TmpStrBytes.Length<=aLimitedNum)
                    return aSrcStr;
                else
                {
                    byte[] LStrBytes=null;
                    int NeedStrNum=0;
                    if (TmpStrBytes[aLimitedNum]>127)
                    {
                        LStrBytes=new byte[aLimitedNum+1];
                        NeedStrNum=aLimitedNum+1;
                    }
                    else
                    {
                        LStrBytes=new byte[aLimitedNum];
                        NeedStrNum=aLimitedNum;
                    }
                    Array.Copy(TmpStrBytes,LStrBytes,NeedStrNum);
                    return System.Text.Encoding.GetEncoding("GB2312").GetString(LStrBytes);
                }
            }

    4.其它网上流传

    public static string SubComment(string original, int width)
            {
                int len = original.Length;
                if (len < width)
                    return original;
                int clen = 0;//当前长度 
                int cwidth = 0;//当前宽度
                while (clen < len && cwidth < width)
                {
                    if ((int) original[clen] > 128)
                        cwidth++;
                    clen++;
                    cwidth++;
                }
                return original.Substring(0, clen);
            }
  • 相关阅读:
    xtrabackup原理1
    mydumper原理3
    mydumper原理1
    mydumper原理2
    MYSQL数据库管理之权限管理
    percona-toolkit工具包的使用教程
    Percona-Galera-Monitoring-Template监控模板说明
    mysql优化--博森瑞
    percona-toolkit -1
    innobackupex --slave-info参数的含义和适用场景
  • 原文地址:https://www.cnblogs.com/twh/p/1402101.html
Copyright © 2011-2022 走看看