Javascript String operation package

Source: Internet
Author: User

Core code:
Copy codeThe Code is 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
* Of how many times the second string appears in the first.
* (Returns the number of times a substring appears in a string)
* @ Param inStr The string to be searched.
* @ Param inSearchStr The string to search.
* @ 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 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.
* (This function is used to block or retain characters in "inCharList", depending on the parameter "ipiporallow ")
* @ 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 required iporallow =
* "Strip"), or (B) the list of characters that will
* NOT be stripped from inStr (when required iporallow =
"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.
* (This function is used to determine whether the inString is a character in inCharList or to make the opposite judgment, depending on the inFromExcept parameter)
* @ Param inString The string to validate.
* @ Param inCharList A list of characters that is either (a) the only
* Characters allowed in inString (when infrom=t
* Is = "from_list") or (B) the only characters that
* Cannot appear in inString (when infrom=t is
* = "Not_from_list ").
* @ Param infrom=t 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
* Optional t thos in inCharList.
* @ Return True if inString only contains valid characters,
* As listed in inCharList when infrom==
* "From_list", false if not, or true if inString does
* Not containt any of the characters listed in
* InCharList when infrom=t = "not_from_list ".
*/
Jscript. string. strContentValid = function (inString, inCharList, infromworkflow t ){
If (inString = null | inCharList = null | infrom=t = null |
InString = "" | inCharList = ""){
Return false;
}
Infromresponse T = infromresponse T. toLowerCase ();
Var I;
If (infrom=t = "from_list "){
For (I = 0; I <inString. length; I ++ ){
If (inCharList. indexOf (inString. charAt (I) =-1 ){
Return false;
}
}
Return true;
}
If (infrom=t = "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
* It to be more precise) with a specified new substring. The substrings
* Can of course be single characters.
* (This function replaces all inOld values 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.
* (Removes spaces 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.
* (Corresponding to the above function, the white space characters are removed 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:
SubstrCount ()-Count how many times the string "wo" appears in the string "How much wood wowould a woodchuck chuck if a woodchuck cocould chuck wood" (5) <br> stripChars ()-Strip the characters 'aeio 'from the string "Denny Crane is cool! ", Then strip all characters comment t 'denycranc' <br> strContentValid ()-Validate that the string" 12345 "only contains numbers (true ), and then that the string "abc123" does not contain the character B (false) <br> replace () -Replace "wood" with "metal" in the string "How much wood wocould a woodchuck chuck if a woodchuck cocould chuck wood" <br> leftTrim () -Trim leading spaces from the string "test" (will display length before (6) and after (4) <br> rightTrim () -Trim trailing spaces from the string "test" (will display length before (6) and after (4) <br> fullTrim () -Trim both leading and trailing spaces from the string "test" (will display length before (8) and after (4) <br> breakLine () -Break up the string "All work and no play makes Homer go crazy" into 10 character chunks <br>
[Ctrl + A select all Note: If you need to introduce external Js, You need to refresh it to execute]

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.