Javascript string string manipulation package _javascript Tips

Source: Internet
Author: User
Core code:
Copy Code code as follows:

/**
* 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
* How-many times the second string appears in the
* (returns the number of occurrences of a substring in a string)
* @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 would take take an input string and either the strip any
* character that appears in a given list of characters, or'll strip any
* Character that does isn't appear in a given list of characters.
* (This function is used to mask or preserve characters in "incharlist", depending on the parameter "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
* 'll be stripped from inStr (when instriporallow = =
* "Strip"), or (b) the list of characters that would
* Not being 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 are a given string either only contains characters
* From a list, or does to contain any characters from a given list.
* (This function is used to determine whether Instring is a character in incharlist, or to make the opposite judgment, depending on the parameter infromexcept)
* @param instring the string to validate.
* @param incharlist A List of characters this is either (a)
* characters allowed in instring (when infromexcept
* is = = = "From_list") or (b) the only characters that
* cannot appear in instring while Infromexcept is
* = = = "Not_from_list").
* @param infromexcept is "from_list", then instring
* contain characters from incharlist. When it 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 is more precise) with a specified new substring. The substrings
* Can of course is single characters.
* (This function replaces the function of the string and replaces all inold in insrc with 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.
* (remove white space characters from the left of the 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.
* (in relation to the above function, remove whitespace characters from the right side of the 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 ().

Demo Area
<script> if (typeof jscript = = ' undefined ') {JScript = function () {}} jscript.string = function () {} JSCR Ipt.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 < 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 (). Jscript.string.strContentValid = function (instring, incharlist, infromexcept) {if (instring = null | | incharlist = NUL L | | 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 (i)) = =-1) {return false; } return true; } if (infromexcept = = "Not_from_list") {for (i = 0; i < instring.length; i++) {if Incharlist.indexof (Instring.cha RAt (i))!=-1 {return false; } return true; }//End Strcontentvalid (). 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 (). 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 (). </script> <div id= "Jscript_string_div" >substrcount ()-count How many times the string "Wo" appears in the Str ing "how much wood would a woodchuck chuck if a woodchuck could chuck Wood" (5) <br><br>stripchars ()-strip the characters ' Aeio ' from the string "Denny Crane are cool!", then Strip all Char Acters EXCEPT ' Denycran ' <br><br>strcontentvalid ()-validate the string "12345" only contains numbers (t Rue), and then that's the string "abc123" does not contain the character B (false) <br><br>replace ()-replace " Wood "with" Metal ' in the string ' How much wood would a woodchuck chuck if a woodchuck could chuck Wood ' <br><b R>lefttrim ()-trim leading spaces from the string "  test" ('ll display length before (6) and after (4) &LT;BR&G T;<br>righttrim ()-trim trailing spaces from the string "test " (would display length before (6) and after (4)) <br><br>fulltrim ()-trim both leading and trailing spaces from the string "  test " ('ll display Length before (8) and after (4)) <br><br>breakline ()-break up the string "All work and no play makes Homer g o Crazy "into charactER chunks <br><br></div> <div id= "C_jquery_test" ></div>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.