Java format string, inserts the specified string at the specified location, compatible with both Chinese and English, and special characters, such as: line break, to resolve issues such as generating PDF line breaks

Source: Internet
Author: User

Cause: The following code is only possible because HTML to PDF does not wrap automatically.

Note: Fully simulate the automatic wrapping of HTML pages!

 Packagetest;Importjava.io.UnsupportedEncodingException;/*** Solve PDF wrap problem, insert specified string at specified location, compatible with Chinese and English and special characters * *@authorXG June **/ Public classApp { Public Static voidMain (string[] args)throwsunsupportedencodingexception {System.out.println (Addstr (Ten, "<br/>", "as Assad DSA Asidifen FLADADASDSJF Asidifen ljdsljkjlfdsklfd What place is Skljdsasfasdfads")); }    /*** Insert Method * *@paramnum * Inserts a string every few characters *@paramSplitstr * String to be specified *@paramSTR * Original String *@returninserts a string after the specified string *@throwsunsupportedencodingexception*/     Public StaticString Addstr (intnum, string splitstr, String str)throwsunsupportedencodingexception {stringbuffer sb=NewStringBuffer (); String Temp=str; intLen =str.length ();  while(Len > 0) {            intIDX =getendindex (temp, num); Sb.append (Temp.substring (0, IDX + 1) . Append (SPLITSTR); Temp= temp.substring (idx + 1); Len=temp.length (); }        returnsb.tostring (); }    /*** Two digits/English * *@paramstr * String *@paramnum * Inserts a string every few characters *@returnint Final Index *@throwsunsupportedencodingexception*/     Public Static intGetendindex (String str,DoubleNumthrowsunsupportedencodingexception {intIDX = 0; Doubleval = 0.00; //Judging whether it is English/Chinese         for(inti = 0; I < str.length (); i++) {            if(String.valueof (Str.charat (i)). GetBytes ("UTF-8"). Length >= 3) {                //Chinese characters or symbolsVal + = 1.00; } Else {                //English characters or symbolsVal + = 0.50; }            if(Val >=num) {idx=i; if(Val-num = = 0.5) {idx= I-1; }                 Break; }        }        if(idx = = 0) {idx= Str.length ()-1; }        returnidx; }}

Effect:

Java format string, inserts the specified string at the specified location, compatible with both Chinese and English, and special characters, such as: line break, to resolve issues such as generating PDF line breaks

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.