Java intercepts string functions. java intercepts strings.

Source: Internet
Author: User

Java intercepts string functions. java intercepts strings.

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:
*/

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

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

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

If (conten (www.111cn.net) ttemp. 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;

From: http://www.111cn.net/jsp/Java/46329.htm


Java truncation string

Public String getString (String str, String str1 ){
Int index = str1.indexOf (str); // str1 indicates the character to be truncated. Str is the truncated character.
Return str. subString (index + 1, str. length );
}

Java string truncation Function

This program first converts the string to a byte array and then intercepts the string based on the length of the string you want to intercept.
Byte bt [] = str. getBytes (); // convert it to a byte array.
If (bt [byteNum] <0) // determine the truncation length. The Truncation length cannot be less than 0.
{
String substrx = new String (bt, 0, -- byteNum );
/* String (byte [] ascii, int hibyte)
Expired. This method cannot correctly convert bytes into characters. From JDK 1.1, the preferred method to complete the conversion is to use the String constructor, which accepts a Character Set Name or uses the default Character Set of the platform. */

// Suggestion: Check the API more when learning.
Reference: jdk 1.5 api

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.