Java long string auto-line (java long string swap)

Source: Internet
Author: User
Tags email address example

One requirement: A long string needs to be automatically folded according to a certain width, and an English word cannot be split into two lines, so google will take a look. Someone in stackoverflow recommends apache's org. apache. commons. lang3.text. WordUtils. wrap (String, int, String, boolean) method. As a result, the test is as follows:

                      String input = "Note: If you choose to send this document via fax then be absolutely sure that you have sent it correctly. Faxes are often received back-to-front, blank, or totally illegible.The ICLA is not tied to any employer you may have, so it is recommended to use one's personal email address in the contact details, rather than an @work address.Your Full name will be published unless you provide an alternative Public name. For example if your full name is Andrew Bernard Charles Dickens, but you wish to be known as Andrew Dickens, please enter the latter as your Public name.The email address and other contact details are not published."         System.out.println(WordUtils.wrap(input, 30, ,    }

The output is satisfactory:

     often received back-to-     one's personal email address      example        details are not published.

For WordUtils. the first is the source string, the second is the limit character for each line, and the third is the text added after the cut. The default value of input null is "/r/n ", I need to fold the lines here, so the input is empty. The fourth is whether long words can be split. For example, if a word is too long and exceeds the length of the second parameter, can a word be split into two rows.

Now, the problem is solved, and it is found that foreigners are cattle, and apache is cattle. However, if the input text contains both English and Chinese, what is the effect after the line conversion.

Often received back-to-one's personal email address example published. The city's terrain is low and the plain accounts for 55% of the total area (252 meters.

It is ugly because the width of Chinese is different from that of English. Although each line is separated by 30 characters, the effect is not ideal. I found a solution to this problem in goole, but I cannot describe it too much.

Decide to create a wheel on your own. Rewrite the wrap method of WordUtils. Compatibility with Chinese. After reading the source code of WordUtils, you can start to work with it. The Code is as follows:

           String wrap(String str,           wrap(str, wrapLength, ,          String wrap(String str,                        (str ==                          (newLineStr ==              newLineStr =           (wrapLength < 1             wrapLength = 1           inputLineLength =          offset = 0         StringBuilder wrappedLine =  StringBuilder(inputLineLength + 32         CHString cstr =           (inputLineLength - offset >              (cstr.charAt(offset) == ' '                 offset++                                spaceToWrapAt = cstr.lastIndexOf(' ', wrapLength +               (spaceToWrapAt >=                   offset = spaceToWrapAt + 1             }                   String temp = cstr.substring(offset, wrapLength +                   offset +=             }                  spaceToWrapAt = cstr.indexOf(' ', wrapLength +                  (spaceToWrapAt >= 0                       offset = spaceToWrapAt + 1                 }                       offset =                                     input = input.replaceAll("[^\\x00-\\xff]", "**"           }
  {
                                   .ch_string =             charAt(                      lastIndexOf( ch,                       indexOf( ch,                      String substring( beginIndex,                      String substring(                      getFixedIndex(          findByHalf(index, 0             findByHalf( index,  start,          (index == 0              0          _0_5 = start + (end - start)/2         String _0_5_str = ch_string.substring(0         ((end - start) < 10                       (WordUtils.getStringLengthByByte(_0_5_str) >                      }                           findOneByOne( index,  start,           result = start - 1         ( i=start; i<=end; i++             String temp = ch_string.substring(0              byteLength =             (byteLength >                              } (byteLength ==                              }                 result ++            -1               .ch_string =                       }

The output result is as follows:

-to-'s personal email address%%

The effect is much better. The idea in the Code is that I wrap a Chinese String myself and provide simple substring, charAt and other methods. before calling the String native method, I made a correction to the passed index. The getFixedIndex method is used to obtain the corrected index. The correction principle is to count the number of bytes in the string. The Chinese character counts two bytes, and the English character counts one. If the input index is 3 and the string is "I am a you", the position between "a" and "you" is 3 instead of directly calculating the position by character, I used the split search idea to avoid low efficiency.

This requirement is a real requirement at work. I don't know if someone else has met it. I tried Google and searched some of them. It is difficult to find the answer in this direction, there may also be a problem with the keywords I searched.

I don't know if I have repeatedly invented the wheel. If you have a master who has a better method or a mature jar package that provides this function, I hope you can give me some advice.

 

References:

1. http://commons.apache.org/proper/commons-lang/apidocs/org/apache/commons/lang3/text/WordUtils.html

2. http://www.blogjava.net/nokiaguy/archive/2010/04/11/317982.html

 

 

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.