/**
* Description: Get the length of the string in bytes;
* @ Versions 0.2;
* @ Return returns the length of the string in bytes (for example, "12 to 12", the length of the string is 6 );
*/
String. Prototype. getlength = function (){
VaR text = This. Replace (/[^ \ x00-\ xFF]/g ,"**");
Return text. length;
}
/**
* Description: truncates a string by byte length and adds a suffix.
* @ Param Len: The length to be intercepted. If the length of a string is insufficient, the return value is itself;
* @ Param alt Add a suffix (Optional). The default value is "......";
* @ Return returns the intercepted string;
* @ Requires getlength;
*/
String. Prototype. getterraform = function (Len, alt ){
VaR tempstr = this;
If (this. getlength ()> Len ){
If (! ALT ){
Alt = "......";
}
VaR I = 0;
For (VAR z = 0; Z <Len; Z ++ ){
If (tempstr. charcodeat (z)> 255 ){
I = I + 2;
} Else {
I = I + 1;
}
If (I> = Len ){
Tempstr = tempstr. Slice (0, (Z + 1) + ALT;
Break;
}
}
Return tempstr;
} Else {
Return this + "";
}
}
Use Case:
Alert ("February 5". getterraform (8 ));
Return Value: "1, 2, 3 ......"