Home  >  Article  >  Web Front-end  >  js intercepts string length (Chinese, English)

js intercepts string length (Chinese, English)

巴扎黑
巴扎黑Original
2016-12-06 13:33:12878browse

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);                                                                                                                                                                        

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