Several Methods to intercept long strings.

Source: Internet
Author: User

1. Intercept through CSS. However, we need to differentiate IE, ff, and other browsers.

<Style type = "text/CSS">
. DS
{
Max-width: 95px;/* the width of the layer. */
}
.
{
Display: block;/* defined as block level */
Width: 90px;/* width of the text to be displayed */
Float: Left;/* left alignment */
Overflow: hidden;/* Hide the excess parts. */
White-space: nowrap ;/**/
Padding-Right: 10px;/* The text is 7 pixels away from the right side. */
Text-overflow: ellipsis;/* replace the ellipsis (...) When IE is not displayed */
-O-text-overflow: ellipsis;/* replace the ellipsis (...) in the fields not displayed by opera */
}
. DS: After
{
Content :"...";
}
/* Supports Firefox */</style>

2. Intercept through custom methods

  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 ++ ; // Important: Count the characters whose values in the first p bytes are greater than 127.
N ++ ;
}
If (M %   2   ! =   0 ) N = P +   1 ; // If it is not even: It indicates that the end is a double byte character, and the number of truncated digits is added to 1.
Strinput [ 0 ] = Encode. getstring (bytearr, 0 , N );
Strinput [ 1 ] = Encode. getstring (bytearr, N, bytearr. Length - N );
Return Strinput;
}

3. Intercept through Js.

 

< Script Type = " Text/JavaScript " >

$ (Function() {Myfunction ();})

FunctionMyfunction (){

VaRJj=Document. getelementbyid ("Jj"). Innertext;

If (JJ. Length > 100 ){
Document. getelementbyid ( " Jj " ). Innerhtml = JJ. substring ( 0 , 99 ) +   " <Span id = 'span1'> <a href = '# 'onclick = 'display (0)'>... expand </a> </span> <span id = 'span2' style = 'display: none; '> "   + JJ. substring ( 99 , JJ. length) +   " <A onclick = 'display (1) 'href =' # '> collapse </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>

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.