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.
*(返回字符串中某子串出现的次数)
* @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().

演示区:

if (typeof jscript == 'undefined') {
jscript = function() { }
}
jscript.string = function() { }
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().
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 -1) {
inSrc = inSrc.replace(inOld, inNew);
}
return inSrc;
} // End replace().
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().
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().
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需刷新才能执行]

(0)

相关推荐

  • js处理json以及字符串的比较等常用操作

    js处理json格式的插入.修改.删除,以及字符串的比较等常用操作 demo 1: json格式的插入.删除 复制代码 代码如下: <html> <head> <title></title> <script language="javascript"> function change(){ var obj=document.getElementById("floor"); if (document.getE

  • JavaScript中的字符串操作详解

    一.概述    字符串在JavaScript中几乎无处不在,在你处理用户的输入数据的时候,在读取或设置DOM对象的属性时,在操作cookie时,当然还有更 多....JavaScript的核心部分提供了一组属性和方法用于通用的字符串操作,如分割字符串,改变字符串的大小写,操作子字符串等.    当前的大部分浏览器也能从强大的正则表达式获益,因为它极大地简化了大量的字符串操作任务,不过它也需要你克服一条有些陡峭的学习曲线.在这里,主要是介绍字符串本身的一些操作,正则表达式会在以后的随笔中涉及. 二

  • JavaScript数组的定义及数字操作技巧

    一.数组的介绍 数组中的元素类型可以是数字型.字符串型.布尔型等,甚至也可以是一个数组. 二.定义数组 1.通过数组的构造函数来定义数组: var arr=new Array(); var arr=new Array(size); var arr=new Array(element1,element2,...); 2.直接定义数组: var arr=["字符串",true,13]; ps: 和Object一样,此写法不会调用Array()构造函数. 三.数组元素 1.存取数组元素:通过

  • JS操作XML实例总结(加载与解析XML文件、字符串)

    本文实例讲述了JS操作XML的方法.分享给大家供大家参考,具体如下: 我的xml文件Login.xml如下. <?xml version="1.0" encoding="utf-8" ?> <Login> <Character> <C Text="热血" Value="0"></C> <C Text="弱气" Value="1&qu

  • JavaScript字符串String和Array操作的有趣方法

    字符串和数组在程序编写过程中是十分常用的类型,因此程序语言都会将String和Array作为基本类型,并提供许多字符串和数组的方法来简化对字符串的操作.JavaScript里面也提供了String类型和Array类型,并且有很多基本的String方法和Array方法来方便地对字符串进行合并.查找.替换.截取等处理. JavaScript作为一个脚本语言,又提供了一种动态解析运行的机制,而这特性,又让使得在String操作的时候出现一些结合使用Array的有趣方法.这些方法可能有些偏门有点奇怪,但

  • JavaScript 字符串操作的几种常见方法

    连接字符串 连接字符串 var str1="Javascript字符串连接"+",方法一"; var str2="方法二"; str2+="使用+=连接"; var str3="方法三"; str3+=",多字符串连接"+".同时使用多个字符连接"+",正确!"; var str4="字符串连接"; str4=str4.conc

  • js String对象中常用方法小结(字符串操作)

    1.charCodeAt方法返回一个整数,代表指定位置字符的Unicode编码. strObj.charCodeAt(index) 说明: index将被处理字符的从零开始计数的编号.有效值为0到字符串长度减1的数字. 如果指定位置没有字符,将返回NaN. 例如: var str = "ABC"; str.charCodeAt(0); 结果:65 2.fromCharCode方法从一些Unicode字符串中返回一个字符串. String.fromCharCode([code1[,cod

  • Javascript中字符串和数字的操作方法整理

    1.length – 返回字符串的长度 'abcd'.length; //4 2.Math.ceil(num) – 向上取整,不管小数点后面是多少,哪怕.00001,也会向上进一位. Math.ceil(25.9); //26 Math.ceil(25.5); //26 Math.ceil(25.1); //26 3.Math.floor(num) – 向下取整,不管小数点后面是多少,哪怕.99999,也会向下减一位. Math.floor(25.9); //25 Math.floor(25.5

  • js实现字符串和数组之间相互转换操作

    本文实例介绍了javascript中字符串和数组的相互转换方法,分享给大家供大家参考,具体内容如下 字符串和数组的相互转换操作是非常的重要的,因为在实际编码过程中会经常用到,所以这是必须要掌握的知识点,当然这个知识点并不难,知道了就永远知道了,并不是那种需要充分实践才能够掌握的东西,下面就做一下简单的介绍. 一.字符串转换为数组 此操作会用到split()函数,它能够以指定的字符作为分隔符,将字符串转换成一个数组,实例代码如下: var Str="abc-mng-zhang-mayi"

  • js 字符串操作函数

    concat() – 将两个或多个字符的文本组合起来,返回一个新的字符串. indexOf() – 返回字符串中一个子串第一处出现的索引.如果没有匹配项,返回 -1 . charAt() – 返回指定位置的字符. lastIndexOf() – 返回字符串中一个子串最后一处出现的索引,如果没有匹配项,返回 -1 . match() – 检查一个字符串是否匹配一个正则表达式. substring() – 返回字符串的一个子串.传入参数是起始位置和结束位置. replace() – 用来查找匹配一个

随机推荐