Cut the string according to the specified display length and cut the string

Source: Internet
Author: User

Cut the string according to the specified display length and cut the string

When processing TextView display text in the past two days, you need to cut the string into several segments of the same length and display the text according to the specified length and redundant line breaks, so I made the following small algorithms and wrote them down to remember!

Public static String getChangedString (String inputString, int byteLength) {char [] chars = inputString. toCharArray (); char [] workChars = new char [chars. length * 2]; // I is the badge of the working array, length is the length of the working process, stringLength is the actual length of the character, j is the input character badge int I = 0, stringLength = 0; float length = 0; for (int j = 0; j <chars. length; I ++, j ++) {// if the source string contains a line break, here we need to clear the length calculated during the working process if (chars [j] = '\ n') {length = 0;} try {workChars [I] = chars [j ]; // Process Chinese characters if (new Character (chars [j]). toString (). getBytes ("GBK "). length = 2/* & chars [j]! = '& Chars [j]! = '*/) {Length ++; if (length> = byteLength) {if (chars [j + 1]! = '\ N') {I ++; stringLength ++; workChars [I] =' \ n';} length = 0 ;}} else if (new Character (chars [j]). toString (). getBytes ("GBK "). length = 1) {// process the characters and numbers of spaces. If (chars [j] = '') {length-= 0.5;} else {length + = 0.05 ;}} catch (UnsupportedEncodingException e) {// TODO Auto-generated catch blocke. printStackTrace ();} stringLength ++; length ++; // if the length exceeds the specified length, insert \ nif (length> = byteLength) {if (chars [j + 1]! = '\ N') {I ++; stringLength ++; workChars [I] =' \ n';} length = 0 ;}} string outputString = new String (workChars ). substring (0, stringLength )/*. trim () */; System. out. println (outputString); return outputString ;}

The following are the test code and results:

Public static void main (String [] args) {// TODO Auto-generated method stubString string = "hope, hope, Dongfeng, spring is approaching. \ N "+" is like waking up, And you are glad to open your eyes. The sun blushes and the water rises. \ N "+" The grass was secretly drilled out of the earth, tender and green. In the garden, in the field, look, a large film is full. Sit down, lie down, roll two times, kick a few feet, run a few times, catch a few times back in the game. The wind is light and quiet, and the grass is soft. \ N "+" Peach Tree, apricot tree, and pear tree, you won't let me, I won't let you, It's all full of flowers. Red like fire, pink like Xia, white like snow. The flowers have a sweet taste. When the eyes are closed, the trees seem to be full of peaches, apricots, and pears. Thousands or even hundreds of bees are buzzing with "+", and butterflies fly in and out. Wild flowers are everywhere: they are named and untitled. They are scattered in the grass like eyes, stars, and blinks. \ N "+" "blow the face is not cold Willow Wind", good, like a mother's hand stroked you. The wind brings some new soil breath, mixed with the smell of grass, and the fragrance of various flowers, "+" are brewing in the slightly moist air. The birds will nest in the flowers and young leaves, happy to get up, call friends accompanied to sell crisp throat, sing a wan Song, with the light wind flow to meet. The PIOs of the shepherd's child on the back of the ox were shining brightly. \ N "+" Rain is the most common. It is three or two days. Don't bother. Look, like a Ox hair, like a flower needle, like a thin silk, closely oblique woven, the roof of the people all cage a thin layer of smoke. The leaves are green, and the grass is green to your eyes. In the evening, the light was on, and a little bit of yellow light turned out a quiet and peaceful night. In the country, on the small road, on the side of the stone bridge, there are people who walk slowly with umbrellas, and there are still farmers who work in the ground, wearing yarn and yarn. Their houses are quiet in the rain. \ N "+" the number of kites in the sky gradually increases, and the number of children on the ground increases. In the countryside of the city, every household, old, old, and small, all come out one by one. Let's take a look at each other. "One year is in spring." When I first started, I had time and hope. \ N "+" spring is like a newly launched doll. It is new from the beginning to the foot and grows. \ N "+" spring is like a girl. She smiled and walked. \ N "+" spring is like a strong young man with an iron arm and waist and feet leading us forward. \ N "; getChangedString (string, 60 );}

Test results:


Related Article

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.