JavaScript String : Object
Instance Methods
Returns a String
of length 1
that contains the character at position pos
.
Another way to retrieve a character in a string is this[pos]
.
Example:
RunResults:
Returns the unicode value of the character at position pos
. Use String.fromCharCode()
to generate a string from character code values.
Example:
RunResults:
Returns the UTF-32 code point of starting at pos
. If the UTF-16 value at pos
does not
start a surrogate pair, the returned value will be the UTF-16 value at pos
. Use String.fromCodePoint()
to generate a string from code points.
Example:
RunResults:
Returns true
if searchString
is at the end of this
. If
endIndex
is specified, the last character of searchString
must be at
endIndex - 1
in this
.
Example:
RunResults:
Returns true
if searchString
is somewhere in this
starting the search from
startingIndex
. If startingIndex
is negative, this.length
is added to it
before starting the search.
Example:
RunResults:
Returns the first location of searchString
in this
starting the search from
startingIndex
. Returns -1
if searchString
is not found.
Example:
RunResults:
Returns the location of searchString
in this
starting the search from
startingIndex
by searching backwards through the string. If startingIndex
is not
specified, the search starts from the end of the string. Returns -1
if searchString
is
not found.
Example:
RunResults:
Compares this
to that
. Returns a negative number if this
would sort before
that
, 0
if this
and that
are equal, and a positive number if
this
would sort after that
.
Example:
RunResults:
If regexp
matches this
, returns a new Array
with item 0
equal
to the portion of this
that matched the regular expression, item 1
equal to the first
capturing group in regexp
, item 2
equal to the second capturing group in
regexp
, and so on. The returned Array
will also have an index
property set
to the starting index of the match and an input
property set to this
. If
regexp
doesn't match this
, returns null
. See also String.matchAll() and RegExp.exec().
Example:
RunResults:
You can name capture groups by placing ?<name>
at the start of the group.
The captured values are available on a groups
property on the returned array. Note,
this is new with ECMAScript 2018.
Example:
RunResults:
Returns a normalized form of this
according to form
. form
must be one of
'NFC'
, 'NFD'
, 'NFKC'
, or 'NFKD'
. See https://unicode.org/reports/tr15/.
Example:
RunResults:
Returns a new String
where the first occurrence of searchValue
in this
is
replaced with the value returned from calling replaceFunction
.
The match
parameter to
replaceFunction
is the same as searchValue
, offset
is the index in
this
where searchValue
was found, and string
is this
.
Example:
RunResults:
Returns a new String
where searchValue
matches in this
is
replaced with the value returned from calling replaceFunction
. If
searchValue
is a global RegExp
,
each match in this
will be replaced. Otherwise, just the first match will
be replaced.
The match
parameter to
replaceFunction
is the same as searchValue
,
the capture
parameters are the values of the capture groups in searchValue
(if any),
offset
is the index in this
where searchValue
was found,
and string
is this
.
Example:
RunResults:
Returns a new String
where the first occurrence of searchValue
in this
is
replaced with replaceValue
.
Example:
RunResults:
Returns a new String
where searchValue
is replaced with replaceValue
. If
searchValue
is a global RegExp
, each match in
this
will be replaced. Otherwise, just the first match will be replaced.
Example:
RunResults:
Returns a new String
where all occurrences of searchValue
in this
are
replaced with the value returned from calling replaceFunction
.
The match
parameter to
replaceFunction
is the same as searchValue
, offset
is the index in
this
where searchValue
was found, and string
is this
.
Example:
RunResults:
Returns a new String
where all matches of searchValue
in this
are
replaced with the value returned from calling replaceFunction
.
searchValue
must be a global RegExp
.
The match
parameter to
replaceFunction
is the same as searchValue
,
the capture
parameters are the values of the capture groups in searchValue
(if any),
offset
is the index in this
where searchValue
was found,
and string
is this
.
Example:
RunResults:
Returns a new String
where all occurrences of searchValue
in this
are
replaced with replaceValue
.
Example:
RunResults:
Returns a new String
where all matches of searchValue
are replaced with
replaceValue
. searchValue
must be a global
RegExp
.
Example:
RunResults:
Executes regexp
on this
and returns the index of the first match. Returns
-1
if regexp
does not match this
.
Example:
RunResults:
Returns a new string composed of the section of this
between start
and
end-1
. If endIndex
is not specified, this.length
is used instead. If
startIndex
or endIndex
is negative, this.length
is added to it before
performing the substring. Similar to substring()
.
Example:
RunResults:
Splits this
at separator
into an Array
of String
s. If
limit
is specified, the returned array will contain no more than limit
items.
Example:
RunResults:
Returns true
if searchString
is at the beginning of this
. If
startingIndex
is specified, searchString
must be at startingIndex
in
this
.
Example:
RunResults:
Returns a new string composed of the section of this
between start
and
end-1
. Before performing the operation, substring
may modifiy the effective values of
start
and end
as follows. If both start
and end
are specified,
and end
is less than start
, the values are swapped. If start
is less than
0
, it is replaced with 0
. If end
is negative, this.length
is
added to it before performing the substring. If end
is not specified, this.length
is
used instead. Similar to slice()
.
Example:
RunResults:
Returns a copy of this
where each character is lower case.
Example:
RunResults:
Returns a copy of this
where each character is upper case.
Example:
RunResults:
Returns a copy of this
where each character is lower case.
Example:
RunResults:
Returns a copy of this
where each character is upper case.
Example:
RunResults:
Returns a copy of this
with leading and trailing whitespace removed. See also trimStart()
and trimEnd()
.
Example:
RunResults:
Returns a copy of this
with trailing whitespace removed. See also trim()
and trimStart()
.
Example:
RunResults:
String Methods
Returns a new string composed of characters from the specified UTF-16 values. Each code should be between 0 and
0xFFFF inclusive. Use charCodeAt()
to retrieve character codes from
strings. See also fromCodePoint()
.
Example:
RunResults:
Returns a new string composed of characters from the specified UTF-32 code points. Each code point should be
between 0 and 0x10FFFF inclusive. Use codePointAt()
to retrievecodes from
strings. See also fromCharCode()
.