Javascript String 字符串操作包
/**
* 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.
*(返回字符串中某子串出現(xiàn)的次數(shù))
* @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.
*(此函數(shù)用來(lái)屏蔽或者保留 "inCharList" 中的字符,取決于參數(shù) "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.
*(此函數(shù)用來(lái)判斷 inString 是否為 inCharList 中的字符,或者進(jìn)行相反的判斷,取決于參數(shù) 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.
*(此函數(shù)進(jìn)行字符串的替換功能,將 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.
*(從字符串的左面開(kāi)始去除空白字符)
* @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.
*(與上面的函數(shù)相對(duì)應(yīng),從字符串的右側(cè)去除空白字符)
* @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().
演示區(qū):
[Ctrl+A 全選 注:引入外部Js需再刷新一下頁(yè)面才能執(zhí)行]
- js 字符串操作函數(shù)
- js String對(duì)象中常用方法小結(jié)(字符串操作)
- js字符串操作總結(jié)(必看篇)
- JavaScript中的字符串操作詳解
- JS基于正則截取替換特定字符之間字符串操作示例
- JavaScript 字符串操作的幾種常見(jiàn)方法
- JavaScript中常見(jiàn)的字符串操作函數(shù)及用法匯總
- js與jQuery實(shí)現(xiàn)獲取table中的數(shù)據(jù)并拼成json字符串操作示例
- 深入淺析JavaScript字符串操作方法 slice、substr、substring及其IE兼容性
- JavaScript實(shí)現(xiàn)的反序列化json字符串操作示例
- js字符串操作方法實(shí)例分析
- JavaScript字符串處理常見(jiàn)操作方法小結(jié)
相關(guān)文章
JavaScript new對(duì)象的四個(gè)過(guò)程實(shí)例淺析
這篇文章主要介紹了JavaScript new對(duì)象的四個(gè)過(guò)程,結(jié)合實(shí)例形式簡(jiǎn)單分析了javascript面向?qū)ο蟪绦蛟O(shè)計(jì)中new對(duì)象的四個(gè)過(guò)程相關(guān)原理與實(shí)現(xiàn)方法,需要的朋友可以參考下2018-07-07基于Bootstrap實(shí)現(xiàn)的下拉菜單手機(jī)端不能選擇菜單項(xiàng)的原因附解決辦法
小編使用bootstrap做的下拉菜單在電腦瀏覽器中可以正常使用,在手機(jī)瀏覽器中能彈出下拉列表卻不能選擇列表中的菜單項(xiàng),怎么回事,如何解決呢?下面小編給大家分享下具體原因及解決辦法,一起看下吧2016-07-07JavaScript DOM節(jié)點(diǎn)操作方法總結(jié)
下面小編就為大家?guī)?lái)一篇JavaScript DOM節(jié)點(diǎn)操作方法總結(jié)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-08-08JSON數(shù)據(jù)中存在單個(gè)轉(zhuǎn)義字符“\”的處理方法
這篇文章主要介紹了JSON數(shù)據(jù)中存在單個(gè)轉(zhuǎn)義字符“\”的處理方法,在這里反斜杠(又稱(chēng)右斜杠"\"),還表示轉(zhuǎn)義字符,字符串中不能成單出現(xiàn)。具體內(nèi)容詳情大家跟隨腳本之家小編一起看看吧2018-07-07echarts柱狀堆疊圖實(shí)現(xiàn)示例(圖例和x軸都是動(dòng)態(tài)的)
一些柱形圖在數(shù)據(jù)量比較多的時(shí)候,橫向排列受到擠壓,導(dǎo)致柱形圖,變的非常細(xì),影響整體的效果,下面這篇文章主要給大家介紹了關(guān)于echarts柱狀堆疊圖(圖例和x軸都是動(dòng)態(tài)的)的相關(guān)資料,需要的朋友可以參考下2023-04-04javascript 導(dǎo)出數(shù)據(jù)到Excel(處理table中的元素)
最近做的項(xiàng)目中有個(gè)要求,需要將數(shù)據(jù)導(dǎo)出到Excel中,關(guān)于這個(gè)就不是什么問(wèn)題,網(wǎng)上的資料很多??僧?dāng)Table中有Input(text)之類(lèi)的元素是怎么辦?2009-12-12如何用JavaScript學(xué)習(xí)算法復(fù)雜度
這篇文章主要介紹了如何用JavaScript學(xué)習(xí)算法復(fù)雜度,對(duì)算法感興趣的同學(xué),一定要看一下2021-04-04