Wrap resolve page because punctuation can not appear at the beginning of each line, leading to early wrap, appeared in the middle of the gap problem

We can only handle relatively short text. For each first character string length can be taken off line, then a line or a space character, punctuation so that will not force a line in the front, because there is a blank line capital character, the character is a blank line when Poem.

Below I posted about the interception of the string used method

function cutStr(str,L){
     was result = ''
         strlen = str.length, // string length
         . Chrlen = str.replace (/ [^ \ x00- \ xff] / g, '**') length; // length in bytes

     if(chrlen<=L){return str;}

     for (var i = 0, j = 0; i <strlen; i ++) {
         var chr = str.charAt (i);
         if(/[\x00-\xff]/.test(chr)){
             j ++; // ascii codes 0-255, a character is one byte in length
         }else{
             j + = 2; // ascii code other than 0-255, a is the length of two bytes character
         }
         + After if (j <= L) {// if the current character after adding, if the total byte length is less than equal to L, then the current real character result
             result += chr;
         } Else {// otherwise the value of L is already closest to the case where the result does not split the character described directly back
             return result;
         }
     }
 }

 

Guess you like

Origin www.cnblogs.com/javalisong/p/11697025.html