核心代码:
复制代码 代码如下:
/**
* jscript.string package
* this package contains utility functions for working with strings.
*/
if (typeof jscript == 'undefined') {
jscript = function() { }
}
jscript.string = function() { }
/**
* this function searches a string for another string and returns a count
* of how many times the second string appears in the first.
*(返回字符串中某子串出现的次数)
* @param instr the string to be searched.
* @param insearchstr the string to search for.
* @return the number of times insearchstr appears in instr,
* or 0 if instr or insearchstr is null or a blank string.
*/
jscript.string.substrcount = function(instr, insearchstr) {
if (instr == null || instr == ||
insearchstr == null || insearchstr == ) {
return 0;
}
var splitchars = instr.split(insearchstr);
return splitchars.length - 1;
} // end substrcount().
/**
* this function will take take an input string and either strip any
* character that appears in a given list of characters, or will strip any
* character that does not appear in a given list of characters.
*(此函数用来屏蔽或者保留 incharlist 中的字符,取决于参数 instriporallow)
* @param instr the string to strip characters.
* @param instriporallow either the value strip or allow.
* @param incharlist this is either (a) the list of characters that
* will be stripped from instr (when instriporallow ==
* strip), or (b) the list of characters that will
* not be stripped from instr (when instriporallow ==
allow.
* @return the value of instr after characters have been
* stripped as specified.
*/
jscript.string.stripchars = function(instr, instriporallow, incharlist) {
if (instr == null || instr == ||
incharlist == null || incharlist == ||
instriporallow == null || instriporallow == ) {
return ;
}
instriporallow = instriporallow.tolowercase();
var outstr = ;
var i;
var j;
var nextchar;
var keepchar;
for (i = 0; i nextchar = instr.substr(i, 1);
if (instriporallow == allow) {
keepchar = false;
} else {
keepchar = true;
}
for (j = 0; j checkchar = incharlist.substr(j, 1);
if (instriporallow == allow && nextchar == checkchar) {
keepchar = true;
}
if (instriporallow == strip && nextchar == checkchar) {
keepchar = false;
}
}
if (keepchar == true) {
outstr = outstr + nextchar;
}
}
return outstr;
} // end stripchars().
/**
* this function can check is a given string either only contains characters
* from a list, or does not contain any characters from a given list.
*(此函数用来判断 instring 是否为 incharlist 中的字符,或者进行相反的判断,取决于参数 infromexcept)
* @param instring the string to validate.
* @param incharlist a list of characters that is either (a) the only
* characters allowed in instring (when infromexcept
* is == from_list) or (b) the only characters that
* cannot appear in instring (when infromexcept is
* == not_from_list).
* @param infromexcept when this is from_list, then instring may only
* contain characters from incharlist. when this is
* not_from_list, then instring can contain any character
* except thos in incharlist.
* @return true if instring only contains valid characters,
* as listed in incharlist when infromexcept ==
* from_list, false if not, or true if instring does
* not containt any of the characters listed in
* incharlist when infromexcept == not_from_list.
*/
jscript.string.strcontentvalid = function(instring, incharlist, infromexcept) {
if (instring == null || incharlist == null || infromexcept == null ||
instring == || incharlist == ) {
return false;
}
infromexcept = infromexcept.tolowercase();
var i;
if (infromexcept == from_list) {
for (i = 0; i if (incharlist.indexof(instring.charat(i)) == -1) {
return false;
}
}
return true;
}
if (infromexcept == not_from_list) {
for (i = 0; i if (incharlist.indexof(instring.charat(i)) != -1) {
return false;
}
}
return true;
}
} // end strcontentvalid().
/**
* this function replaces a given substring of a string (all occurances of
* it to be more precise) with a specified new substring. the substrings
* can of course be single characters.
*(此函数进行字符串的替换功能,将 insrc 中的 inold 全部替换为 innew)
* @param insrc the string to replace substring(s) in.
* @param inold the substring to replace.
* @param innew the new substring to insert.
* @return the value of insrc with all occurances of inold replaced
* with innew.
*/
jscript.string.replace = function(insrc, inold, innew) {
if (insrc == null || insrc == || inold == null || inold == ||
innew == null || innew == ) {
return ;
}
while (insrc.indexof(inold) > -1) {
insrc = insrc.replace(inold, innew);
}
return insrc;
} // end replace().
/**
* function to trim whitespace from the beginning of a string.
*(从字符串的左面开始去除空白字符)
* @param instr the string to trim.
* @return the trimmed string, or null if null or a blank string was
* passed in.
*/
jscript.string.lefttrim = function(instr) {
if (instr == null || instr == ) {
return null;
}
var j;
for (j = 0; instr.charat(j) == ; j++) { }
return instr.substring(j, instr.length);
} // end lefttrim().
/**
* function to trim whitespace from the end of a string.
*(与上面的函数相对应,从字符串的右侧去除空白字符)
* @param instr the string to trim.
* @return the trimmed string, or null if null or a blank string was
* passed in.
*/
jscript.string.righttrim = function(instr) {
if (instr == null || instr == ) {
return null;
}
var j;
for (j = instr.length - 1; instr.charat(j) == ; j--) { }
return instr.substring(0, j + 1);
} // end righttrim().
/**
* function to trim whitespace from both ends of a string.
*
* @param instr the string to trim.
* @return the trimmed string, or null if null or a blank string was
* passed in.
*/
jscript.string.fulltrim = function(instr) {
if (instr == null || instr == ) {
return ;
}
instr = this.lefttrim(instr);
instr = this.righttrim(instr);
return instr;
} // end fulltrim().
演示区:
substrcount()-count how many times the string wo appears in the string how much wood would a woodchuck chuck if a woodchuck could chuck wood (5)
stripchars()-strip the characters 'aeio' from the string denny crane is cool!, then strip all characters except 'denycran'
strcontentvalid()-validate that the string 12345 only contains numbers (true), and then that the string abc123 does not contain the character b (false)
replace()-replace wood with metal in the string how much wood would a woodchuck chuck if a woodchuck could chuck wood
lefttrim()-trim leading spaces from the string test (will display length before (6) and after (4)
righttrim()-trim trailing spaces from the string test (will display length before (6) and after (4))
fulltrim()-trim both leading and trailing spaces from the string test (will display length before (8) and after (4))
breakline()-break up the string all work and no play makes homer go crazy into 10 character chunks
[ctrl+a 全选 注:如需引入外部js需刷新才能执行]