Strings
var str1 = "double quotes";
var ch = str1[2], len = str1.length; // string as characters array
var str2 = 'single quotes';
var str3 = new String("creates via String object");
var str4 = "nesting 'quotes' allowed";
var str5 = "my long long\
string";
var str6 = "preferred way for long string " +
"is using + operator";
var str7 = "escape \"sequences\" supported";
// string templates (ES6)
var str8 = ` ${any_var_expression} ${2+3}`;
var str9 = ` long string with
template`;
escape sequences
esc. seq. | description |
---|---|
\" | Inserts a double quote. |
\' | Inserts a single quote. |
\\ | Inserts a backslash. |
\n | Inserts a new line. |
\r | Inserts a carriage return. |
\v | Inserts a vertical tab. |
\t | Inserts a tab. |
\f | Form feed. |
\uXXXX | Inserts a unicode character with code XXXX (4 hex digits). Possible values are in range of 0x0000–0xFFFF. |
methods
Index or position of character in string is zero-based.
Parameters of methods in square braces [] are optional and can be omitted.
Methods
method | description |
---|---|
indexOf |
Finds first occurrence of text. Parameters:
|
lastIndexOf
|
Finds last occurrence of text. Parameters:
|
startsWith |
Returns true if the given characters are found at the beginning of the string; otherwise, false. Default value of the pos parameter is 0. |
endsWith(strSearch [,len]) | Returns true if the given characters are found at the end of the string; otherwise, false. The len parameter specifies the desired length of this string. |
match(regexp) |
Retrieves the result of matching a string against a regular expression.
Returns null if no matches are found, or Array with following content:
|
matchAll(regexp) | Returns an iterator of all results matching a string against a regular expression, including capturing groups. The regexp parameter must have the g flag, otherwise a TypeError will be thrown. |
search(regexp) | Searches first match between the regular expression and the given string. Returns position of match, or -1 if nothing found. |
replace(regexp|substr, newSubstr|func) |
Returns a new string with some or all matches of a pattern replaced with a replacement string specified by the newSubstr or generated by the func function. If pattern is a string, only the first occurrence will be replaced. The newSubstr parameter can include the following special replacement patterns:
|
replaceAll(regexp|substr, newSubstr|func) |
Returns a new string with all matches of a pattern replaced by a replacement.
If the replacement string specified by regular expression, it must have flag g. |
charAt(pos) | Returns character at pos position. |
charCodeAt(pos) | Returns the unicode of the character at pos position. |
slice(from[,to ]) |
Extracts a section of a string and returns it as a new string,
without modifying the original string. Parameters:
|
substring (from [, to]) |
Extracts a part of a string and returns it as a new string.
Parameters:
|
substr (from [, len]) |
Extracts a part of a string and returns it as a new string.
Parameters:
|
split ([delim[, limit]]) |
Splits string into an array of strings.
Parameters:
|
concat (str [, ...strN]) | Concatenates the string parameters into one new string. |
repeat(count) | Returns a new string containing the specified number of copies of the given string. |
trim() | Returns new string with stripped whitespace from both sides. Most browsers support methods trimStart(), trimLeft(), trimEnd() and trimRight for removing whitespace from one side. |
padStart(toLen [, padstr]) | Pads the current string with another string until the resulting string reaches the given length. The padding is applied from the start of the current string. Default value of the padstr parameter is space. |
padEnd(toLen [, padstr]) | Pads the current string with another string until the resulting string reaches the given length. The padding is applied from the end of the current string. Default value of the padstr parameter is space. |
toUpperCase() | Returns new string with upper case characters. |
toLowerCase() | Returns new string with lower case characters. |