Home > Web Front-end > JS Tutorial > body text

js prototype intercepts string function_javascript skills

WBOY
Release: 2016-05-16 18:30:53
Original
876 people have browsed it

Prototype is some APIs written in Javascript, including extensions to classes in Javascript such as String, Array, etc. You can embed the JS file and use it directly.

Copy code The code is as follows:

String.prototype.strLen = function() {
var len = 0;
for (var i = 0; i < this.length; i ) {
if (this.charCodeAt(i) > 255 || this.charCodeAt(i) < 0) len = 2; else len ;
}
return len;
}
//Split the string into characters and store them in the array
String.prototype.strToChars = function(){
var chars = new Array();
for (var i = 0; i < this.length; i ){
chars[i] = [this.substr(i, 1), this.isCHS(i)];
}
String.prototype.charsArray = chars;
return chars;
}
//Determine whether a character is a Chinese character
String.prototype.isCHS = function(i){
if (this.charCodeAt(i) > 255 || this.charCodeAt(i) < 0)
return true;
else
return false;
}
//Intercept string (from start byte to end byte)
String.prototype.subCHString = function(start, end){
var len = 0;
var str = "";
this.strToChars( );
for (var i = 0; i < this.length; i ) {
if(this.charsArray[i][1])
len = 2;
else
len ;
if (end < len)
return str;
else if (start < len)
str = this.charsArray[i][0];
}
return str;
}
//Intercept string (intercept length bytes from start byte)
String.prototype.subCHStr = function(start, length){
return this. subCHString(start, start length);
}
var li= document.getElementsByName("listtitle");
for(var i=0;i{
li[i].innerHTML=li[i].innerHTML.subCHStr(0,28) "...";
}

prototype is used to inherit the prototype object , mainly to save memory space.
Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template