Type: String

A collection of the String Object methods and functions.

Смотрите также:

Function: String.from

Returns the passed parameter as a String.

Syntax:

String.from(arg);

Аргументы:

  1. arg - (mixed) The argument to return as a string.

Возвращает:

  • (string) The argument as a string.

Пример:

String.from(2); // returns '2'
String.from(true); // returns 'true'

Function: String.uniqueID

Generates a unique ID

Syntax:

String.uniqueID();

Возвращает:

  • (string) A unique ID.

Пример:

String.uniqueID();

String method: test

Searches for a match between the string and a regular expression. For more information see MDC Regexp:test.

Syntax:

myString.test(regex[, params]);

Аргументы:

  1. regex - (mixed) The string or regular expression you want to match the string with.
  2. params - (string, optional) If first parameter is a string, any parameters you want to pass to the regular expression ('g' has no effect).

Возвращает:

  • (boolean) true, if a match for the regular expression is found in this string.
  • (boolean) false if is not found

Examples:

'I like cookies'.test('cookie'); // returns true
'I like cookies'.test('COOKIE', 'i'); // returns true (ignore case)
'I like cookies'.test('cake'); // returns false

Смотрите также:

String method: contains

Checks to see if the string passed in is contained in this string. If the separator parameter is passed, will check to see if the string is contained in the list of values separated by that parameter.

Syntax:

myString.contains(string[, separator]);

Аргументы:

  1. string - (string) The string to search for.
  2. separator - (string, optional) The string that separates the values in this string (e.g. Element classNames are separated by a ' ').

Возвращает:

  • (boolean) true if the string is contained in this string
  • (boolean) false if not.

Examples:

'a bc'.contains('bc'); // returns true
'a b c'.contains('c', ' '); // returns true
'a bc'.contains('b', ' '); // returns false

String method: trim

Trims the leading and trailing spaces off a string.

Syntax:

myString.trim();

Возвращает:

  • (string) The trimmed string.

Examples:

'    i like cookies     '.trim(); // returns 'i like cookies'

String method: clean

Removes all extraneous whitespace from a string and trims it (String:trim).

Syntax:

myString.clean();

Возвращает:

  • (string) The cleaned string.

Examples:

' i      like     cookies      \n\n'.clean(); // returns 'i like cookies'

String method: camelCase

Converts a hyphenated string to a camelcased string.

Syntax:

myString.camelCase();

Возвращает:

  • (string) The camelcased string.

Examples:

'I-like-cookies'.camelCase(); // returns 'ILikeCookies'

String method: hyphenate

Converts a camelcased string to a hyphenated string.

Syntax:

myString.hyphenate();

Возвращает:

  • (string) The hyphenated string.

Examples:

'ILikeCookies'.hyphenate(); // returns '-i-like-cookies'

String method: capitalize

Converts the first letter of each word in a string to uppercase.

Syntax:

myString.capitalize();

Возвращает:

  • (string) The capitalized string.

Examples:

'i like cookies'.capitalize(); // returns 'I Like Cookies'

String method: escapeRegExp

Escapes all regular expression characters from the string.

Syntax:

myString.escapeRegExp();

Возвращает:

  • (string) The escaped string.

Examples:

'animals.sheep[1]'.escapeRegExp(); // returns 'animals\.sheep\[1\]'

String method: toInt

Parses this string and returns a number of the specified radix or base.

Syntax:

myString.toInt([base]);

Аргументы:

  1. base - (number, optional) The base to use (defaults to 10).

Возвращает:

  • (number) The number.
  • (NaN) If the string is not numeric, returns NaN.

Examples:

'4em'.toInt(); // returns 4
'10px'.toInt(); // returns 10

Смотрите также:

String method: toFloat

Parses this string and returns a floating point number.

Syntax:

myString.toFloat();

Возвращает:

  • (number) The float.
  • (NaN) If the string is not numeric, returns NaN.

Examples:

    '95.25%'.toFloat(); // returns 95.25
    '10.848'.toFloat(); // returns 10.848

Смотрите также:

String method: hexToRgb

Converts a hexadecimal color value to RGB. Input string must be in one of the following hexadecimal color formats (with or without the hash). '#ffffff', #fff', 'ffffff', or 'fff'

Syntax:

myString.hexToRgb([array]);

Аргументы:

  1. array - (boolean, optional) If true is passed, will output an array (e.g. [255, 51, 0]) instead of a string (e.g. 'rgb(255, 51, 0)').

Возвращает:

  • (string) A string representing the color in RGB.
  • (array) If the array flag is set, an array will be returned instead.

Examples:

'#123'.hexToRgb(); // returns 'rgb(17, 34, 51)'
'112233'.hexToRgb(); // returns 'rgb(17, 34, 51)'
'#112233'.hexToRgb(true); // returns [17, 34, 51]

String method: rgbToHex

Converts an RGB color value to hexadecimal. Input string must be in one of the following RGB color formats. 'rgb(255, 255, 255)', or 'rgba(255, 255, 255, 1)'

Syntax:

myString.rgbToHex([array]);

Аргументы:

  1. array - (boolean, optional) If true is passed, will output an array (e.g. ['ff', '33', '00']) instead of a string (e.g. '#ff3300').

Возвращает:

  • (string) A string representing the color in hexadecimal, or transparent if the fourth value of rgba in the input string is 0.
  • (array) If the array flag is set, an array will be returned instead.

Examples:

'rgb(17, 34, 51)'.rgbToHex(); // returns '#112233'
'rgb(17, 34, 51)'.rgbToHex(true); // returns ['11', '22', '33']
'rgba(17, 34, 51, 0)'.rgbToHex(); // returns 'transparent'

Смотрите также:

String method: substitute

Substitutes keywords in a string using an object/array. Removes undefined keywords and ignores escaped keywords.

Syntax:

myString.substitute(object[, regexp]);

Аргументы:

  1. object - (mixed) The key/value pairs used to substitute a string.
  2. regexp - (regexp, optional) The regexp pattern to be used in the string keywords, with global flag. Defaults to /\?{([^}]+)}/g .

Возвращает:

  • (string) - The substituted string.

Examples:

var myString = '{subject} is {property_1} and {property_2}.';
var myObject = {subject: 'Jack Bauer', property_1: 'our lord', property_2: 'saviour'};
myString.substitute(myObject); // returns Jack Bauer is our lord and saviour

String method: stripScripts

Strips the String of its tags and anything in between them.

Syntax:

myString.stripScripts([evaluate]);

Аргументы:

  1. evaluate - (boolean, optional) If true is passed, the scripts within the String will be evaluated.

Возвращает:

  • (string) - The String without the stripped scripts.

Examples:

var myString = "<script>alert('Hello')</script>Hello, World.";
myString.stripScripts(); // returns 'Hello, World.'
myString.stripScripts(true); // alerts 'Hello', then returns 'Hello, World.'

Эта документация распостраняется на правах Attribution-NonCommercial-ShareAlike 3.0 License.
Оригинал документации на английском.
© Linux.ria.ua, 2008-2024