Java precisely intercepts a string and obtains the character function with the specified length before the string.
The contentstr = contenttemp. substring (0,150) function that gets the first part of the string using Java. Ensure that the maximum length cannot exceed the length of the string. Below are some of my implementation code and the code for searching online:
/*
* 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
*/
// 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 );
----------------------
It is often encountered during development that the string is too long to be fully displayed.
At this time, we need to extract the required length, followed by a ellipsis or other characters.
Because the Chinese character occupies two bytes while the English character occupies one byte, it is often unsatisfactory to simply judge the number of characters.
The following method is used to determine the character type to intercept, and the effect is acceptable :)
If you have other solutions, please post them and learn together :)
**************************************** ******************************
Private string STR;
Private int counterofdoublebyte;
Private byte B [];
/**
* Set the string to be limited in length
* @ Param STR the string to be restricted in length
*/
Public void setlimitlengthstring (string Str ){
This. Str = STR;
}
/**
* @ Param Len the length to be displayed (<font color = "red"> Note: The length is in bytes, and one Chinese character is in two bytes </font>)
* @ Param symbol refers to the characters used to indicate the omitted information, such as "..." and ">>>.
* @ Return returns the processed string
*/
Public String getlimitlengthstring (INT Len, string symbol) throws unsupportedencodingexception {
Counterofdoublebyte = 0;
B = Str. getbytes ("GBK ");
If (B. Length <= Len)
Return STR;
For (INT I = 0; I <Len; I ++ ){
If (B [I] <0)
Counterofdoublebyte ++;
}
If (counterofdoublebyte % 2 = 0)
Return new string (B, 0, Len, "GBK") + symbol;
Else
Return new string (B, 0, len-1, "GBK") + symbol;
}
-------------------
/***//**
* Truncate a string by Byte Length
* @ Param STR string parameter to be intercepted
* @ Param tocount refers to the length of the captured bytes.
* @ Param more string
* @ Return returns the intercepted string
*/
Public string substring (string STR, int tocount, String More )...{
Int reint = 0;
String restr = "";
If (STR = NULL)
Return "";
Char [] tempchar = Str. tochararray ();
For (int kk = 0; (KK <tempchar. Length & tocount> reint); KK ++ )...{
String S1 = Str. valueof (tempchar [Kk]);
Byte [] B = s1.getbytes ();
Reint + = B. length;
Restr + = tempchar [Kk];
}
If (tocount = reint | (tocount = reint-1 ))
Restr + = more;
Return restr;
}
========================
/**
* The first tocount character of the string.
*
* @ Param STR the processed string
* @ Param tocount truncation Length
* @ Param more suffix string
* @ Version 2004.11.24
* @ Author zhulx
* @ Return string
*/
Public static string substring (string STR, int tocount, String More)
{
Int reint = 0;
String restr = "";
If (STR = NULL)
Return "";
Char [] tempchar = Str. tochararray ();
For (int kk = 0; (KK <tempchar. Length & tocount> reint); KK ++ ){
String S1 = Str. valueof (tempchar [Kk]);
Byte [] B = s1.getbytes ();
Reint + = B. length;
Restr + = tempchar [Kk];
}
If (tocount = reint | (tocount = reint-1 ))
Restr + = more;
Return restr;
}
------------------------
Returns the real length of a string and the string function with a fixed length.
// Truncate a fixed-length substring. ssource is the string. ilen is the 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;
}