Prototype JavaScript framework

class String

Description

Extensions to the built-in String class.

Prototype enhances the String object with a series of useful methods for ranging from the trivial to the complex. Tired of stripping trailing whitespace? Try String#strip. Want to replace replace? Have a look at String#sub and String#gsub. Need to parse a query string? We have String#toQueryParams.

Instance methods

  • blank

    String#blank() -> Boolean

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

  • camelize

    String#camelize() -> String

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

  • capitalize

    String#capitalize() -> String

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

  • dasherize

    String#dasherize() -> String

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

  • empty

    String#empty() -> Boolean

    Checks if the string is empty.

  • endsWith

    String#endsWith(substring) -> Boolean

    Checks if the string ends with substring.

  • escapeHTML

    String#escapeHTML() -> String

    Converts HTML special characters to their entity equivalents.

  • evalJSON

    String#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

    String#evalScripts() -> Array

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

  • extractScripts

    String#extractScripts() -> Array

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

  • gsub

    String#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

    String#include(substring) -> Boolean

    Check if the string contains a substring.

  • inspect

    String#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

    String#interpolate(object[, pattern]) -> String

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

  • isJSON

    String#isJSON() -> Boolean

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

  • scan

    String#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

    String#startsWith(substring) -> Boolean

    Checks if the string starts with substring.

  • strip

    String#strip() -> String

    Strips all leading and trailing whitespace from a string.

  • stripScripts

    String#stripScripts() -> String

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

  • stripTags

    String#stripTags() -> String

    Strips a string of any HTML tag.

  • sub

    String#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. The pattern can be a string or a regular expression.

  • succ

    String#succ() -> String

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

  • times

    String#times(count) -> String

    Concatenates the string count times.

  • toArray

    String#toArray() -> Array

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

  • toJSON

    String#toJSON() -> String

    Returns a JSON string.

  • toQueryParams

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

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

    Alias of:

  • truncate

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

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

  • underscore

    String#underscore() -> String

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

  • unescapeHTML

    String#unescapeHTML() -> String

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

  • unfilterJSON

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

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

Class methods

  • interpret

    String.interpret(value) -> String

    Coerces value into a string. Returns an empty string for null.