时间:2021-05-28
核心代码:
复制代码 代码如下:
/**
* 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 < inStr.length; i++) {
nextChar = inStr.substr(i, 1);
if (inStripOrAllow == "allow") {
keepChar = false;
} else {
keepChar = true;
}
for (j = 0; j < inCharList.length; 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 < inString.length; i++) {
if (inCharList.indexOf(inString.charAt(i)) == -1) {
return false;
}
}
return true;
}
if (inFromExcept == "not_from_list") {
for (i = 0; i < inString.length; 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需再刷新一下页面才能执行]
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
在C++中则把字符串封装成了一种数据类型string,可以直接声明变量并进行赋值等字符串操作。以下是C字符串和C++中string的区别:C字符串string对
一、String类String类在java.lang包中,java使用String类创建一个字符串变量,字符串变量属于对象。java把String类声明的fin
JavaScript的数据类型Number:整形,浮点型String:字符型数据,JavaScript不区分单个字符和字符串,任何字符或字符串都可以用又引号或单
String字符串对象1.介绍 String对象,对字符串进行操作,如:截取一段子串、查找字符串/字符、转换大小写等等。2.定义方式2.1newString(
定义和用法eval()函数可计算某个字符串,并执行其中的的JavaScript代码。语法eval(string)参数描述string必需。要计算的字符串,其中含