Java string truncation Function

Source: Internet
Author: User

The function used to intercept strings in java is also a substring function. It is a bit like a string Intercept function in SQL. Next I will introduce you to the substring function in detail.

Substring

Public String substring (int beginIndex) returns a new String, which is a substring of this String. The substring starts with a character at the specified index until the end of the string.
For example:
"Unhappy". substring (2) returns "happy"
"Harbison". substring (3) returns "bison"
"Emptiness". substring (9) returns "" (an empty string)
Parameters:
BeginIndex-the index at the beginning (including ).
Return Value:
The specified substring.
Throw:
IndexOutOfBoundsException-If beginIndex is negative or greater than the length of this String object

Example

/*

* If the content is too long, the xml file may be too large and loading is too slow.

* From the seo perspective, all outputs are good for search engines, but the content is usually not too much.

* To prevent the css from being subject to space wrapping, the space is replaced by a regular expression. Therefore, the first 100 characters are truncated and the content displayed on the page is subject to css control.

 

* Zdz works

The Code is as follows: Copy code

*/

// Str. trim (). replaceAll ("\ s + ","");

String contenttemp = rs. getString (contentName). trim (). replaceAll ("\ s + ","");

// NpfDebug. print (contenttemp. length ());

If (contenttemp. length ()> 100) {// intercept if the length is greater than 100

Contenttemp = contenttemp. substring (0,100 );

// NpfDebug. print ("contenttemp. length ()> 100? "+ Contenttemp. length () +" n "+ contentStr );

}

Rsbody. append (beforCONTENT );

Rsbody. append (contenttemp );

Rsbody. append (endCONTENT );

 

Returns the real length of a string and the string function with a fixed length.


Function getInterceptedStr (sSource, iLen)

{

If (sSource. replace (/[^ x00-xff]/g, "xx"). length <= iLen)

{

Return sSource;

}

Var ELIDED = "";

 

Var str = "";

Var l = 0;

Var schar;

For (var I = 0; schar = sSource. charAt (I); I ++)

{

Str + = schar;

L ++ = (schar. match (/[^ x00-xff]/)! = Null? 2: 1 );

If (l> = iLen-ELIDED. length)

{

Break;

}

}

Str + = ELIDED;

 

Return str;

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.