mobile-graphic-easel-examples-assets-StringUtils.js / js
Returns everything after the first occurrence of the provided character in the string.
parameter: p_begin The character or sub-string.
returns: String
Returns everything after the last occurence of the provided character in this.
parameter: p_char The character or sub-string.
returns: String
Determines whether the specified string begins with the specified prefix.
parameter: p_begin The prefix that will be tested against the string.
returns: Boolean
Returns everything before the first occurrence of the provided character in the string.
parameter: p_begin The character or sub-string.
returns: String
Returns everything before the last occurrence of the provided character in the string.
parameter: p_begin The character or sub-string.
returns: String
Returns everything after the first occurance of p_start and before the first occurrence of p_end in this.
parameter: p_start The character or sub-string to use as the start index.
parameter: p_end The character or sub-string to use as the end index.
returns: String
Capitallizes the first word in a string or all words.
parameter: p_all (optional) Boolean value indicating if we should capitalize all words or only the first.
returns: String
Determines whether the specified string contains any instances of p_char.
parameter: p_char The character or sub-string we are looking for.
returns: Boolean
Levenshtein distance (editDistance) is a measure of the similarity between two strings, The distance is the number of deletions, insertions, or substitutions required to transform p_source into p_target.
parameter: p_source The source string.
parameter: p_target The target string.
returns: uint
Determines whether the specified string ends with the specified suffix.
parameter: p_end The suffix that will be tested against the string.
returns: Boolean
Determines whether the specified string contains text.
returns: Boolean
Determines whether the specified string contains any characters.
returns: Boolean
Determines whether the specified string is numeric.
returns: Boolean
Pads this with specified character to a specified length from the left.
parameter: p_padChar Character for pad.
parameter: p_length Length to pad to.
returns: String
Pads this with specified character to a specified length from the right.
parameter: p_padChar Character for pad.
parameter: p_length Length to pad to.
returns: String
Properly cases' the string in "sentence format".
returns: String.
Removes all instances of the remove string in the input string.
parameter: p_remove The string that will be removed from the input string.
parameter: p_caseSensitive An optional boolean indicating if the replace is case sensitive. Default is true.
returns: String
Removes extraneous whitespace (extra spaces, tabs, line breaks, etc) from the specified string.
returns: String
Returns the specified string in reverse character order.* *
returns: String */ String.prototype.reverse = function() { return this.split('').reverse().join(''); } Returns the specified string in reverse word order.
returns: StringString.prototype.reverseWords = function() { return this.split(/\s+/).reverse().join(' '); } Determines the percentage of similiarity, based on editDistance
parameter: p_target The target string.
returns: NumberString.prototype.similarity = function(p_target) { var ed = StringUtils.editDistance(this, p_target); var maxLen = Math.max(this.length, p_target.length); if (maxLen == 0) { return 1; } else { return (1 - ed/maxLen); } } Remove's all html tags from a string
returns: StringString.prototype.stripTags = function() { return this.replace(/<\/?[^>]+>/igm, ''); } Replaces instances of the form {digit} or {name} in string with corresponding arguments or values.
parameter: string0..stringN Strings or
parameter: object Object
returns: StringString.prototype.supplant = function() { var str = this; if(arguments[0] instanceof Object) { for (var n in arguments[0]) { str = str.replace(new RegExp('\\{'+n+'\\}', 'g'), arguments[0][n]); } } else { var l = arguments.length; for (var i=0; i<l; i++) { str = str.replace(new RegExp('\\{'+i+'\\}', 'g'), arguments[i]); } } return str; } Swaps the casing of a string.
returns: StringString.prototype.swapCase = function() { return this.replace(/(\w)/, StringUtils._swapCase); } Removes whitespace from the front and the end of the specified string.
returns: StringString.prototype.trim = function() { return this.replace(/^\s+|\s+ Removes whitespace from the front (left-side) of the specified string.
returns: StringStringUtils.trimLeft = function(str) { return str.replace(/^\s+/, ''); } String.prototype.trimLeft = function() { return StringUtils.trimLeft(this); } Removes whitespace from the end (right-side) of the specified string.
returns: StringStringUtils.trimRight = function(str) { return str.replace(/\s+/, ''); } String.prototype.trimRight = function () { return StringUtils.trimLeft(this); } Returns a string truncated to a specified length with optional suffix
parameter: p_len The length the string should be shortend to
parameter: p_suffix (optional, default=...) The string to append to the end of the truncated string.
returns: StringString.prototype.truncate = function(p_len, p_suffix) { if (p_suffix == null) { p_suffix = '...'; } if (p_len == 0) { p_len = this.length; } p_len -= p_suffix.length; var trunc = this; if (trunc.length > p_len) { trunc = trunc.substr(0, p_len); if (/[^\s]/.test(trunc.charAt(p_len))) { trunc = StringUtils.trimRight(trunc.replace(/\w+/, '')); } trunc += p_suffix; } return trunc; } Determins the number of words in a string.
returns: uintString.prototype.wordCount = function() { return this.match(/\b\w+\b/g).length; } String.prototype.countOf = function(p_char, p_caseSensitive) { p_caseSensitive = p_caseSensitive === true; var c = StringUtils.escapePattern(p_char); var flags = (!p_caseSensitive) ? 'ig' : 'g'; var match = this.match(new RegExp(c, flags)); return match==null?0:match.length; } /* **************************************************************** */ /* These are helper methods used by some of the above methods. */ /* **************************************************************** */ StringUtils.escapePattern = function(p_pattern) { // RM: might expose this one, I've used it a few times already. return p_pattern.replace(/(\]|\[|\{|\}|\(|\)|\*|\+|\?|\.|\\)/g, '\$1'); } StringUtils.prototype._quote = function() { switch (this) { case "\\": return "\\\\"; case "\r": return "\\r"; case "\n": return "\\n"; case '"': return '\\"'; } return null; } StringUtils._upperCase = function(p_char) { return p_char.toUpperCase(); } StringUtils._swapCase = function(p_char) { var lowChar = p_char.toLowerCase(); var upChar = p_char.toUpperCase(); switch (p_char) { case lowChar: return upChar; case upChar: return lowChar; default: return p_char; } }
[]readme course(s) prefaceI 1 2II 3 4III 5 6 7IV 8 9 10V 11 12 afterthought(s)appendix reference(s) example(s)resource(s) _![]()
(C) Æliens 04/09/2009
You may not copy or print any of this material without explicit permission of the author or the publisher. In case of other copyright issues, contact the author.