zoukankan      html  css  js  c++  java
  • 字符串过长,截取的几种方法。

    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>
  • 相关阅读:
    gc buffer busy/gcs log flush sync与log file sync
    给Oracle年轻的初学者的几点建议
    Android 编程下帧动画在 Activity 启动时自动运行的几种方式
    Android 编程下 Touch 事件的分发和消费机制
    Java 编程下 static 关键字
    Java 编程下 final 关键字
    Android 编程下模拟 HOME 键效果
    Why Are Thread.stop, Thread.suspend, Thread.resume and Runtime.runFinalizersOnExit Deprecated ?
    Extjs4 大型项目目录结构重构
    [转]SQLServer 2008 允许远程连接的配置方法
  • 原文地址:https://www.cnblogs.com/gengaixue/p/2125999.html
Copyright © 2011-2022 走看看