String

Overview

The String object is used to represent and manipulate a sequence of characters.

For more information see: String (MDN).

Properties Summarized

Type
Name
Summary

Gives the length of the string.

Methods Summarized

Type
Name
Summary

returns a copy of the string embedded within an anchor <A> tag set.

returns a copy of the string embedded within an <BIG> tag set.

returns a copy of the string embedded within an <BLINK> tag set.

returns a copy of the string embedded within an <B> tag set.

returns a character of the string.

returns a decimal code of the char in the string.

returns a non-negative integer that is the Unicode code point value at the given position.

returns a string that appends the parameter string to the string.

returns a string that appends the parameter string to the string.

Determines whether a string ends with the characters of a specified string, returning true or false as appropriate.

Determines whether a string ends with the characters of a specified string, returning true or false as appropriate.

returns a boolean that checks if the given string is equal to the string

returns a boolean that checks if the given string is equal to the string ignoring case

returns a copy of the string embedded within an anchor <TT> tag set.

returns a copy of the string embedded within an <FONT> tag set, the color param is assigned the the color attribute.

returns a copy of the string embedded within an <FONT> tag set, The size param is set to the SIZE attribute

Static method that returns a string created from the specified sequence of UTF-16 code units.

Static method that returns a string created by using the specified sequence of code points.

Determines whether one string may be found within another string.

returns the found index of the given string in string.

returns a copy of the string embedded within an <I> tag set

returns the found index of the given string in string from the end.

returns a copy of the string embedded within an <A> tag set.

returns an array of strings within the current string that matches the regexp.

Returns the Unicode Normalization Form of the string.

Returns the Unicode Normalization Form of the string.

The padEnd() method pads the current string with a given string (repeated, if needed) so that the resulting string reaches a given length.

The padEnd() method pads the current string with a given string (repeated, if needed) so that the resulting string reaches a given length.

The padStart() method pads the current string with another string (multiple times, if needed) until the resulting string reaches the given length.

The padStart() method pads the current string with another string (multiple times, if needed) until the resulting string reaches the given length.

Constructs and returns a new string which contains the specified number of copies of the string on which it was called, concatenated together.

returns a new string where the matches of the given regexp are replaced by the return value of the function.

returns a new string where the matches of the given reg exp are replaced by newSubStr.

returns a new string where the first match of the given substr is replaced by the return value of the function.

returns a new string where the first match of the given substr is replaced by newSubStr.

returns an index where the first match is found of the regexp

returns a substring of the string.

returns a substring of the string.

returns a copy of the string embedded within an <SMALL> tag set.

returns an array of objects whose elements are segments of the current string.

returns an array of objects whose elements are segments of the current string.

Determines whether a string begins with the characters of a specified string, returning true or false as appropriate.

Determines whether a string begins with the characters of a specified string, returning true or false as appropriate.

returns a copy of the string embedded within an <STRIKE> tag set.

returns a copy of the string embedded within an <SUB> tag set.

returns a substring of the string from the start with the number of chars specified.

returns a substring of the string from the start with the number of chars specified.

Returns a substring of the string from the start index until the end index.

Returns a substring of the string from the start index until the end index.

returns a copy of the string embedded within an <SUP> tag set.

returns a string with all lowercase letters of the current string.

returns a string with all uppercase letters of the current string.

Returns the string stripped of whitespace from both ends.

Removes whitespace from the ending of a string and returns a new string, without modifying the original string.

Removes whitespace from the beginning of a string and returns a new string, without modifying the original string.

Properties Detailed

length

Gives the length of the string.

Type Number

Sample

string.length;

Methods Detailed

anchor(nameAttribute)

returns a copy of the string embedded within an anchor <A> tag set.

Parameters

Returns: String

Sample

string.anchor();

big()

returns a copy of the string embedded within an <BIG> tag set.

Returns: String

Sample

string.big();

returns a copy of the string embedded within an <BLINK> tag set.

Returns: String

Sample

string.blink();

bold()

returns a copy of the string embedded within an <B> tag set.

Returns: String

Sample

string.bold();

charAt(index)

returns a character of the string.

Parameters

Returns: Number

Sample

string.charAt(integer_position);

charCodeAt(index)

returns a decimal code of the char in the string.

Parameters

Returns: Number

Sample

string.charCodeAt(integer_position);

codePointAt(index)

returns a non-negative integer that is the Unicode code point value at the given position. Note that this function does not give the nth code point in a string, but the code point starting at the specified string index.

Parameters

Returns: Number

Sample

string.codePointAt(integer_position);

concat(string2)

returns a string that appends the parameter string to the string.

Parameters

Returns: String

Sample

string.concat(string);

concat(string2, stringN)

returns a string that appends the parameter string to the string.

Parameters

Returns: String

Sample

string.concat(string);

endsWith(searchString)

Determines whether a string ends with the characters of a specified string, returning true or false as appropriate.

Parameters

  • String searchString The characters to be searched for at the end of str.

Returns: String true if the given characters are found at the end of the string; otherwise, false

Sample

var str1 = 'Cats are the best!';
 application.output(str1.endsWith('best', 17));

endsWith(searchString, length)

Determines whether a string ends with the characters of a specified string, returning true or false as appropriate.

Parameters

  • String searchString The characters to be searched for at the end of str.

  • Number length If provided, it is used as the length of str. Defaults to str.length.

Returns: String true if the given characters are found at the end of the string; otherwise, false

Sample

var str1 = 'Cats are the best!';
 application.output(str1.endsWith('best', 17));

equals(other)

returns a boolean that checks if the given string is equal to the string

Parameters

Returns: Boolean

Sample

string.equals(string);

equalsIgnoreCase(other)

returns a boolean that checks if the given string is equal to the string ignoring case

Parameters

Returns: Boolean

Sample

string.equalsIgnoreCase(string);

fixed()

returns a copy of the string embedded within an anchor <TT> tag set.

Returns: String

Sample

string.fixed();

fontcolor(color)

returns a copy of the string embedded within an <FONT> tag set, the color param is assigned the the color attribute.

Parameters

Returns: String

Sample

string.fontcolor(color);

fontsize(size)

returns a copy of the string embedded within an <FONT> tag set, The size param is set to the SIZE attribute

Parameters

Returns: String

Sample

string.fontsize(size);

fromCharCode(num)

Static method that returns a string created from the specified sequence of UTF-16 code units.

Parameters

  • Array num A sequence of numbers that are UTF-16 code units. The range is between 0 and 65535 (0xFFFF). Numbers greater than 0xFFFF are truncated. No validity checks are performed.

Returns: String A string of length N consisting of the N specified UTF-16 code units.

Sample

String.fromCharCode(0x2014); // returns "—"
String.fromCharCode(65, 66, 67); // returns "ABC"

fromCodePoint(num)

Static method that returns a string created by using the specified sequence of code points. String.fromCharCode() cannot return supplementary characters (i.e. code points 0x010000 – 0x10FFFF) by specifying their code point. Instead, it requires the UTF-16 surrogate pair for that. String.fromCodePoint(), on the other hand, can return 4-byte supplementary characters, as well as the more common 2-byte BMP characters, by specifying their code point (which is equivalent to the UTF-32 code unit).

RangeError is thrown if an invalid Unicode code point is given (e.g. "RangeError: NaN is not a valid code point").

Parameters

  • Array num A sequence of code points.

Returns: String A string created by using the specified sequence of code points.

Sample

String.fromCodePoint(42); // "*"
String.fromCodePoint(65, 90); // "AZ"
String.fromCodePoint(0x2f804); // "\uD87E\uDC04"
String.fromCodePoint(-1); // RangeError
String.fromCodePoint(3.14); // RangeError

includes()

Determines whether one string may be found within another string.

Returns: String

Sample

string.includes('foo');

indexOf(searchValue, fromIndex)

returns the found index of the given string in string.

Parameters

Returns: Number

Sample

string.indexOf(string,startPosition);

italics()

returns a copy of the string embedded within an <I> tag set

Returns: String

Sample

string.italics();

lastIndexOf(searchValue, fromIndex)

returns the found index of the given string in string from the end.

Parameters

Returns: Number

Sample

string.lastIndexOf(string,startPosition);

returns a copy of the string embedded within an <A> tag set.

Parameters

Returns: String

Sample

string.link(url);

localeCompare(otherString)

Parameters

Returns: Number

Sample

var s = "Have a nice day!";
application.output(s.localeCompare("Hello"));

match(regexp)

returns an array of strings within the current string that matches the regexp.

Parameters

Returns: Array

Sample

string.match(regexpr);

normalize()

Returns the Unicode Normalization Form of the string. (defaults to "NFC" form)

Returns: String A string containing the Unicode Normalization Form of the given string.

normalize(form)

Returns the Unicode Normalization Form of the string. form param can be one of "NFC", "NFD", "NFKC", or "NFKD", specifying the Unicode Normalization Form. If omitted or undefined, "NFC" is used.

These values have the following meanings:

"NFC" Canonical Decomposition, followed by Canonical Composition. "NFD" Canonical Decomposition. "NFKC" Compatibility Decomposition, followed by Canonical Composition. "NFKD" Compatibility Decomposition.

Parameters

  • String form param can be one of "NFC", "NFD", "NFKC", or "NFKD",

Returns: String A string containing the Unicode Normalization Form of the given string.

Sample

var string1 = '\u00F1';           // �
var string2 = '\u006E\u0303';     // �

string1 = string1.normalize('NFD');
string2 = string2.normalize('NFD');

application.output(string1 === string2); // true
application.output(string1.length);      // 2
application.output(string2.length);      // 2

padEnd(targetLength)

The padEnd() method pads the current string with a given string (repeated, if needed) so that the resulting string reaches a given length. The padding is applied from the end of the current string. The default value used for padding is the unicode "space" character (U+0020) - if no padString argument is used.

Parameters

  • Number targetLength The length of the resulting string once the current str has been padded. If the value is less than or equal to str.length, then str is returned as-is.

Returns: String A String of the specified targetLength with spaces applied at the end of the current str.

Sample

string.padEnd(10, '*');

padEnd(targetLength, padString)

The padEnd() method pads the current string with a given string (repeated, if needed) so that the resulting string reaches a given length. The padding is applied from the end of the current string.

Parameters

  • Number targetLength The length of the resulting string once the current str has been padded. If the value is less than or equal to str.length, then str is returned as-is.

  • String padString The string to pad the current str with. If padString is too long to stay within the targetLength, it will be truncated from the end. The default value is the unicode "space" character (U+0020).

Returns: String A String of the specified targetLength with the padString applied at the end of the current str.

Sample

string.padEnd(10, '*');

padStart(targetLength)

The padStart() method pads the current string with another string (multiple times, if needed) until the resulting string reaches the given length. The padding is applied from the start of the current string. The default value used for padding is the unicode "space" character (U+0020) - if no padString argument is used.

Parameters

  • Number targetLength The length of the resulting string once the current str has been padded. If the value is less than or equal to str.length, then str is returned as-is.

Returns: String A String of the specified targetLength with spaces applied from the start.

Sample

string.padStart(10);

padStart(targetLength, padString)

The padStart() method pads the current string with another string (multiple times, if needed) until the resulting string reaches the given length. The padding is applied from the start of the current string.

Parameters

  • Number targetLength The length of the resulting string once the current str has been padded. If the value is less than or equal to str.length, then str is returned as-is.

  • String padString The string to pad the current str with. If padString is too long to stay within the targetLength, it will be truncated from the end. The default value is the unicode "space" character (U+0020).

Returns: String A String of the specified targetLength with padString applied from the start.

Sample

string.padStart(10, '*');

repeat(count)

Constructs and returns a new string which contains the specified number of copies of the string on which it was called, concatenated together.

Parameters

  • Number count An integer between 0 and +Infinity, indicating the number of times to repeat the string.

Returns: String A new string containing the specified number of copies of the given string.

Sample

var str = 'abc'.repeat(2); // 'abcabc'

replace(regexp, function)

returns a new string where the matches of the given regexp are replaced by the return value of the function. The function parameter is the function to be invoked to create the new substring (to put in place of the substring received from parameter #1).

Parameters

Returns: String

Sample

//the callback definition
function replacer(match, p1, p2, p3, offset, string){
		// match is the matched substring
		// p1 is non-digits, p2 digits, and p3 non-alphanumerics
		// offset is the offset of the matched substring within the total string being examined
		// string is the total string being examined
 	return [p1, p2, p3].join(' - ');
}
// using replace method with replacer callback
newString = "abc12345#$*%".replace(/([^\d]*)(\d*)([^\w]*)/, replacer);

replace(regexp, newSubStr)

returns a new string where the matches of the given reg exp are replaced by newSubStr.

Parameters

Returns: String

Sample

string.replace(regexp,newSubStr);
//var re = /(\w+)\s(\w+)/;
//var str = "John Smith";
//var newstr = str.replace(re, "$2, $1");
//application.output(newstr);

replace(substr, function)

returns a new string where the first match of the given substr is replaced by the return value of the function. The function parameter is the function to be invoked to create the new substring (to put in place of the substring received from parameter #1).

Parameters

Returns: String

Sample

// the callback definition
function replacer(match){
		return match.toUpperCase()
}
// using replace method with replacer callback
var newString = "abc".replace("a", replacer);

replace(substr, newSubStr)

returns a new string where the first match of the given substr is replaced by newSubStr.

Parameters

Returns: String

Sample

string.replace(substr,newSubStr);

search(regexp)

returns an index where the first match is found of the regexp

Parameters

Returns: Number

Sample

string.search(regexpr);

slice(beginSlice)

returns a substring of the string.

Parameters

Returns: String

Sample

string.slice(start,end);

slice(beginSlice, endSlice)

returns a substring of the string.

Parameters

Returns: String

Sample

string.slice(start,end);

small()

returns a copy of the string embedded within an <SMALL> tag set.

Returns: String

Sample

string.small();

split(separator, limit)

returns an array of objects whose elements are segments of the current string.

Parameters

  • RegExp separator Specifies the string which denotes the points at which each split should occur. If separator is an empty string, str is converted to an array of characters.

  • Number limit Optional integer specifying a limit on the number of splits to be found.

Returns: String

Sample

var myString = 'Hello 1 word. Sentence number 2.';
var splits = myString.split(new RegExp(/(\d)/), 2);
application.output(splits); //prints [Hello , 1]

split(separator, limit)

returns an array of objects whose elements are segments of the current string.

Parameters

  • String separator Specifies the string which denotes the points at which each split should occur. If separator is an empty string, str is converted to an array of characters.

  • Number limit Optional integer specifying a limit on the number of splits to be found.

Returns: String

Sample

var myString = 'Hello 1 word. Sentence number 2.';
var splits = myString.split(' ');
application.output(splits);

startsWith(searchString)

Determines whether a string begins with the characters of a specified string, returning true or false as appropriate.

Parameters

  • String searchString The characters to be searched for at the start of this string.

Returns: String true if the given characters are found at the beginning of the string; otherwise, false

Sample

var str1 = 'Cats are the best!';
 application.output(str1.startsWith('Cats'));

startsWith(searchString, position)

Determines whether a string begins with the characters of a specified string, returning true or false as appropriate.

Parameters

  • String searchString The characters to be searched for at the start of this string.

  • Number position The position in this string at which to begin searching for searchString. Defaults to 0.

Returns: String true if the given characters are found at the beginning of the string; otherwise, false

Sample

var str1 = 'Cats are the best!';
 application.output(str1.startsWith('Cats'));

strike()

returns a copy of the string embedded within an <STRIKE> tag set.

Returns: String

Sample

string.strike();

sub()

returns a copy of the string embedded within an <SUB> tag set.

Returns: String

Sample

string.sub();

substr(start)

returns a substring of the string from the start with the number of chars specified.

Parameters

Returns: String

Sample

string.substr(start, number_of_chars);

substr(start, length)

returns a substring of the string from the start with the number of chars specified.

Parameters

Returns: String

Sample

string.substr(start, number_of_chars);

substring(indexA)

Returns a substring of the string from the start index until the end index.

Parameters

Returns: String

Sample

string.substring(start, end);

substring(indexA, indexB)

Returns a substring of the string from the start index until the end index.

Parameters

Returns: String

Sample

string.substring(start, end);

sup()

returns a copy of the string embedded within an <SUP> tag set.

Returns: String

Sample

string.sup();

toLocaleLowerCase()

Returns: String

Sample

var s = "Have a nice day!";
application.output(s.toLocaleLowerCase());

toLocaleUpperCase()

Returns: String

Sample

var s = "Have a nice day!";
application.output(s.toLocaleUpperCase());

toLowerCase()

returns a string with all lowercase letters of the current string.

Returns: String

Sample

string.toLowerCase();

toUpperCase()

returns a string with all uppercase letters of the current string.

Returns: String

Sample

string.toUpperCase();

trim()

Returns the string stripped of whitespace from both ends.

Returns: String

Sample

string.trim();

trimEnd()

Removes whitespace from the ending of a string and returns a new string, without modifying the original string.

Returns: String

Sample

string.trimEnd();

trimStart()

Removes whitespace from the beginning of a string and returns a new string, without modifying the original string.

Returns: String

Sample

string.trimStart();

Last updated