// $Id: string.js,v 1.4 2002/02/20 18:42:46 nwiggins Exp $ // // $Log: string.js,v $ // Revision 1.4 2002/02/20 18:42:46 nwiggins // // # removes script tags - forces usage as a javascript src include. no php includes // // Revision 1.3 2001/07/18 23:07:57 jneil // Fixed some problems. // // Revision 1.2 2001/07/02 06:11:18 vhegde // Revision 1.1 2001/05/21 15:53:53 jneil // Initial Upload. // // // whitespace characters: ' ', '\t', '\r', '\n' var whitespace = " \t\n\r"; // isEmpty: Check whether string strVal is empty function isEmpty(strVal) { return ((strVal == null) || (strVal.length == 0)); } // isWhitespace: Check if string strVal has only the whitespace characters function isWhitespace(strVal) { var nPos = 0; if (isEmpty(strVal)) return false; for (nPos = 0; nPos < strVal.length; nPos++) if (whitespace.indexOf(strVal.charAt(nPos)) == -1) return false; return true; } // rtrim: Right trim the string and return the trimmed value function rtrim(strVal) { var nPos = 0; if (isEmpty(strVal)) return strVal; for (nPos = strVal.length-1; nPos >= 0; nPos--) { if (whitespace.indexOf(strVal.charAt(nPos)) == -1) break; } return (nPos == strVal.length-1 ? strVal.substring(0) : strVal.substring(0, nPos+1)); } // ltrim: Left trim the string and return the trimmed value function ltrim(strVal) { var nPos = 0; if (isEmpty(strVal)) return strVal; for (nPos = 0; nPos < strVal.length; nPos++) { if (whitespace.indexOf(strVal.charAt(nPos)) == -1) break; } return strVal.substring(nPos); } // alltrim: Trim the string on left and right, and return the trimmed value function alltrim(strVal) { return (isEmpty(strVal) ? strVal:ltrim(rtrim(strVal))); } /******************************************************** replace: useful for JavaScript 1.0 and 1.1 where replace is not available. With JavaScript 1.2 (and above) replace function is available. : replaces one occurrence of strVal with strWith in strSrc ********************************************************/ function replace(strSrc, strVal, strWith) { var nPos = 0, strLeft="", strRight=""; // check if empty (or) no string is found to replace if (isEmpty(strSrc) || (strSrc.indexOf(strVal) < 0)) return strSrc; nPos = strSrc.indexOf(strVal); strLeft = strSrc.substring(0, nPos); nPos += strVal.length; strRight = strSrc.substring(nPos); return (strLeft + strWith + strRight); } // replaceall : replace all occurrences of strVal with strWith in strSrc function replaceall(strSrc, strVal, strWith) { var strBuffer=strSrc; while (strBuffer.indexOf(strVal) >= 0) strBuffer = replace(strBuffer, strVal, strWith); return (strBuffer); } // occurs: return no of occurrences of strVal within strSrc function occurs(strVal, strSrc) { var nCnt = 0, strBuffer=strSrc; while (strBuffer.indexOf(strVal) >= 0) { strBuffer = replace(strBuffer, strVal, ""); nCnt++; } return (nCnt); } // replicate: returns string that contains strVal repeated nCnt times function replicate(strVal, nCnt) { var i = 0, strBuffer = ""; for (i = 0; i < nCnt; i++) strBuffer += strVal; return (strBuffer); } /************************************************************** stuff: returns string after replacing nCnt characters (starting from nStartPos) with strReplacement in strSrc. NOTE: starting position is 0 **************************************************************/ function stuff(strSrc, nStartPos, nCnt, strReplacement) { var strLeft= "", strRight=""; // check boundary values if (nCnt < 0 || nStartPos < 0 || (nStartPos > strSrc.length-1)) return strSrc; strLeft = strSrc.substring(0, nStartPos); strRight = strSrc.substring(nStartPos+nCnt); return (strLeft + strReplacement + strRight); }
rss
Карта