Home  >  Article  >  Web Front-end  >  js, intercept the length of the string, string, length js intercept the length of the string (Chinese and English)

js, intercept the length of the string, string, length js intercept the length of the string (Chinese and English)

巴扎黑
巴扎黑Original
2016-11-25 13:58:271185browse

js intercepts the length of the string. When intercepting, the Chinese is calculated as 2 bytes.

function worldLimit(className, factLength){

$('.'+className).each(function(){
var content = $(this ).text();
var allLength = 0;
for (var i = 0; i < content.length; i++) {
var c = content.charCodeAt(i);
//Single byte plus 1
if ((c >= 0x0001 && c <= 0x007e) || (0xff60 <= c && c <= 0xff9f)) {
                                                                                                         1
                                                                                                                                                                                                                                    0; i < content.length; i++) {
        var c = content.charCodeAt(i);
                                                                                                                                                                            subLength++ ;
                                                                                                                                          ;
                                                                                                                                                                                 
             }
}
                $(this).text(subContent);                                                                                                         $(this).

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:js format timestampNext article:js format timestamp