Java splits strings according to the specified length (one is a pure English string and the other is a mix of English and Chinese strings)
Recently, string cutting is used to implement a document reading function. Simply put, a local txt file is opened, displayed on the interface, and a start/pause button is provided. At the beginning, the text is automatically moved to red, and the speed can be controlled by yourself. Just like the lyrics, one row is automatically moved, and the color-changing row is always in the middle of the display control. Why is string cutting used? The reason is that the number of words displayed on the screen of different sizes is different from that displayed on the screen without resolution. During implementation, I thought of cutting strings Based on the Size String Of the display control (here the custom TextView) to adapt to different devices. In the next article, the "Document Reading" function is implemented. This article is used to explain how to cut strings;
A string obtained from a text may be a pure Chinese text or a mix of Chinese and English characters.
When the plain Chinese text is used, it is easier to understand:
/*** Cut the string according to the specified length * @ param inputString the source string to be cut * @ param length the specified length * @ return */public static List
GetDivLines (String inputString, int length) {List
DivList = new ArrayList <> (); int remainder = (inputString. length () % length; // It must be divided into several segments: int number = (int) Math. floor (inputString. length ()/length); for (int index = 0; index <number; index ++) {String childStr = inputString. substring (index * length, (index + 1) * length); System. out. println (childStr); divList. add (childStr);} if (remainder> 0) {String cStr = inputString. substring (number * length, inputString. length (); divList. add (cStr);} return divList ;}
A common (Chinese-English or Chinese-English) method:
/*** Process the input string and divide it into multiple line strings with byteLength as the width. * According to the need, the length of the English characters space is 0.5, the length of Chinese characters is 2 (GBK encoding, UTF-8 is 3), the English letter width is 1.05. * @ Param inputString input String * @ param byteLength split by byteLength length (multiple widths are displayed in one row) * @ return processed String */public static String getChangedString (String inputString, int byteLength) {String outputString = inputString; try {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; (Int j = 0; j <chars. length-1; I ++, j ++) {// if the source string contains a line break, here we need to clear the length calculated during the work process if (chars [j] = '') {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]! = '') {I ++; stringLength ++; workChars [I] ='';} 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 (length> = byteLength) {if (chars [j + 1]! = '') {I ++; stringLength ++; workChars [I] ='';} length = 0 ;}} outputString = new String (workChars ). substring (0, stringLength )/*. trim () */; System. out. println (outputString);} catch (Exception e) {// TODO: handle exceptionLog. e (TextViewHelper, e. toString ();} return outputString ;}
The above is my actual use in my own project. The time is too short for your reference only. When using the tool, you can select a method that suits your needs.
In the process of solving this problem, try other methods, although not implemented, but in the process of solving the problem, as long as you think of the string operation, there are basically all APIs, because they take into account a lot of comprehensiveness, and string operations generally have corresponding APIs in the document. If you find more information on the Internet, you can find it in the API if you cannot find it. There is always a solution to the problem.