• 字符串过长,截取的几种方法。


    1.通过css进行截取。不过要区分ie,ff等浏览器。   

    <style type="text/css">
            .ds
            {
                max- 95px; /*层的宽度。*/
            }
            .as
            {
                display: block; /*定义为块级*/
                90px; /*要显示文字的宽度*/
                float: left; /*左对齐*/
                overflow: hidden; /*超出的部分隐藏起来。*/
                white-space: nowrap; /**/
                padding-right: 10px; /*文字距离右侧7像素。*/
                text-overflow: ellipsis; /* 支持 IE 不显示的地方用省略号...代替*/
                -o-text-overflow: ellipsis; /* 支持 Opera  不显示的地方用省略号...代替*/
            }
            .ds:after
            {
                content: "...";
            }
            /* 支持 Firefox */</style>

    2.通过自定义方法截取

     public static string[] Intercept(string input, int p)
            {
                String[] Strinput 
    =new String[2];
                Encoding encode 
    = Encoding.GetEncoding("gb2312");
                input 
    = input ?? String.Empty;
                
    byte[] byteArr = encode.GetBytes(input);
                
    if (byteArr.Length <= p)
                {
                    Strinput[
    0= input;
                    Strinput[
    1= "";
                    
    return Strinput;
                }

                
    int m = 0, n = 0;
                
    foreach (byte b in byteArr)
                {
                    
    if (n >= p) break;
                    
    if (b > 127) m++//重要一步:对前p个字节中的值大于127的字符进行统计
                    n++;
                }
                
    if (m % 2 != 0) n = p + 1//如果非偶:则说明末尾为双字节字符,截取位数加1
                Strinput[0]= encode.GetString(byteArr, 0, n);
                Strinput[
    1= encode.GetString(byteArr, n, byteArr.Length - n);
                
    return Strinput;
            }

    3.通过js截取。

      <script type="text/javascript">

            $(
    function () { myfunction(); })

            
    function myfunction() {

                
    var jj = document.getElementById("jj").innerText;

                
    if (jj.length>100) {
                    document.getElementById(
    "jj").innerHTML = jj.substring(099+ "<span id='span1'><a href='#' onclick='display(0)'>...展开</a></span><span id='span2' style='display:none;'>" + jj.substring(99, jj.length) + "<a  onclick='display(1)' href='#'> 收起 </a></span>"
                }
            }

            
    function display(id) {
                
    if (id == 0) {
                    document.getElementById(
    "span1").style.display = "none";
                    document.getElementById(
    "span2").style.display = "";
                } 
    else {
                    document.getElementById(
    "span1").style.display = "";
                    document.getElementById(
    "span2").style.display = "none";
                }
            }

        
    </script>
  • 相关阅读:
    个人冲刺8
    个人冲刺7
    个人冲刺6
    个人冲刺5
    个人冲刺4
    个人冲刺阶段3
    个人冲刺阶段2
    课下作业1-扩展阅读
    随手快递app开发的第四天
    随手快递app开发的第三天
  • 原文地址:https://www.cnblogs.com/gengaixue/p/2125999.html
Copyright © 2020-2023  润新知