String

Prototype enhances the String object with a series of useful methods for String.prototype ranging from the trivial to the complex. Tired of stripping trailing whitespaces, try our String#strip method. Want to replace replace ? Have a look at String#sub and String#gsub. Need to parse a query string ? We have just what you need.

Methods

blank
1.5.1

blank() -> Boolean

Check if the string is 'blank', meaning either empty or containing only whitespace.

camelize

camelize() -> string

Converts a string separated by dashes into a camelCase equivalent. For instance, 'foo-bar' would be converted to 'fooBar'.

capitalize

capitalize() -> string

Capitalizes the first letter of a string and downcases all the others.

dasherize

dasherize() -> string

Replaces every instance of the underscore character ("_") by a dash ("-").

empty
1.5.1

empty() -> Boolean

Checks if the string is empty.

endsWith
1.5.1

endsWith(substring) -> Boolean

Checks if the string ends with substring.

escapeHTML

 escapeHTML() -> string

Converts HTML special characters to their entity equivalents.

evalJSON
1.5.1

evalJSON([sanitize = false]) -> object

Evaluates the JSON in the string and returns the resulting object. If the optional sanitize parameter is set to true, the string is checked for possible malicious attempts and eval is not called if one is detected.

evalScripts

evalScripts() -> [returnedValue...]

Evaluates the content of any script block present in the string. Returns an array containing the value returned by each script.

extractScripts

 extractScripts() -> [script...]

Exctracts the content of any script block present in the string and returns them as an array of strings.

gsub

gsub(pattern, replacement) -> string

Returns the string with every occurence of a given pattern replaced by either a regular string, the returned value of a function or a Template string. The pattern can be a string or a regular expression.

include
1.5.1

include(substring) -> Boolean

Check if the string contains a substring.

inspect

inspect([useDoubleQuotes = false]) -> String

Returns a debug-oriented version of the string (i.e. wrapped in single or double quotes, with backslashes and quotes escaped).

interpolate
1.6

interpolate(object[, pattern]) -> string

Treats the string as a Template and fills it with object’s properties.

isJSON
1.5.1.1

isJSON() -> boolean

Check if the string is valid JSON by the use of regular expressions. This security method is called internally.

scan

scan(pattern, iterator) -> string

Allows iterating over every occurrence of the given pattern (which can be a string or a regular expression). Returns the original string.

startsWith
1.5.1

startsWith(substring) -> Boolean

Checks if the string starts with substring.

strip

strip() -> string

Strips all leading and trailing whitespace from a string.

stripScripts

stripScripts() -> string

Strips a string of anything that looks like an HTML script block.

stripTags

stripTags() -> string

Strips a string of any HTML tag.

sub

sub(pattern, replacement[, count = 1]) -> string

Returns a string with the first count occurrences of pattern replaced by either a regular string, the returned value of a function or a Template string. pattern can be a string or a regular expression.

succ

succ() -> string

Used internally by ObjectRange. Converts the last character of the string to the following character in the Unicode alphabet.

times
1.5.1

times(count) -> string

Concatenates the string count times.

toArray

toArray() -> [character...]

Splits the string character-by-character and returns an array with the result.

toJSON
1.5.1

toJSON() -> String

Returns a JSON string.

toQueryParams

toQueryParams([separator = '&']) -> Object

Parses a URI-like query string and returns an object composed of parameter/value pairs.

truncate

truncate([length = 30[, suffix = '...']]) -> string

Truncates a string to the given length and appends a suffix to it (indicating that it is only an excerpt).

underscore

underscore() -> string

Converts a camelized string into a series of words separated by an underscore ("_").

unescapeHTML

 unescapeHTML() -> string

Strips tags and converts the entity forms of special HTML characters to their normal form.

unfilterJSON
1..5.1

unfilterJSON([filter = Prototype.JSONFilter]) -> String

Strips comment delimiters around Ajax JSON or JavaScript responses. This security method is called internally.