JavaScript strings
looking at and manipulating plain text in JavaScript
String
syntax
String
syntaxA String
consists of consecutive alphanumeric characters (this includes whitespace, special characters and even emoji!)
We also encapsulate String
s with quotation marks (either with 'single quotes' or "double quotes", depending on preference)
Concatenating String
s
String
sWe can concatenate, or join, String
s together using the +
operator, e.g.
We can also join any number of String
s together with the concat()
method, by passing each String
per argument:
Searching within String
s with indexOf()
String
s with indexOf()
We can use the indexOf()
method to check whether a substring exists within a String
:
If the substring exists,
indexOf()
will return the position where it existsif it finds the substring in the first character, it will return
0
the index includes whitespace
If the substring does not exist, the
indexOf()
will return-1
If the substring exists in more than one place, the
indexOf()
will just return the index of the first occurrence
Note that indexOf()
is case-sensitive, so that "Word"
and "word"
are not the same thing!
Some examples:
The includes()
method
includes()
methodThe includes()
method determines whether some text (a substring) exists within a string, by returning either true
or false:
Like the indexOf()
method, the includes()
method is also case-sensitive!
This method also has an optional second parameter that checks for the substring only at or past a certain index in the string:
Extracting a substring from a String
with slice()
String
with slice()
To take out a portion of text from a String
, we can use the slice() method which takes in two parameters: an optional starting index and an optional "cutoff" index:
Notice how the "cutoff" index refers to the index at which the substring stops, and would not include the character at that index!)
Also, in the last example, we can see that both parameters are actually optional. However, not having any arguments for the parameter gives a trivial result: the entire string itself!
Changing the case of a String
: toUpperCase()
and toLowerCase
String
: toUpperCase()
and toLowerCase
To change the case (i.e. uppercase or lowercase) of a text, JavaScript has two built-in methods, toUpperCase()
and toLowerCase()
which we can use as such:
We may use these methods when collecting user input from a search query box (some users may use capital letters, others might not) and comparing it with a database entry (from which we would also transform to all lowercase, or to all uppercase!)
Replacing substrings with replace()
replace()
To replace a portion of text with a different text, we can use the replace()
method:
replace()
is case-sensitive and only replaces the first match; the original string also does not change
We need to use something called regular expressions (more details on this later!) in order to add more complexity to our replace()
method:
Notice the replacement (no pun intended) of "quotation marks" with /slashes/ when using regular expressions!
Also:
The "
g
" flag after the slashes means "global replacement"The "
i
" flag after the slashes means "ignore case"Thus, a "
gi
" flag would mean "globally replace, ignore case"
Repeating Strings and substrings with repeat()
repeat()
To repeat a string, just use the built-in method repeat(times)
where times
is any positive number or zero:
The repeat()
method can save us from using loops!
Trimming Strings with trim()
, trimStart()
, trimEnd()
trim()
, trimStart()
, trimEnd()
To remove whitespace (blank characters) at the start or from the end of a string, we can use the trim()
method (and other related methods):
Other methods to remove whitespace include:
trimStart()
, at the beginning of a stringtrimEnd()
, at the end of a string
Last updated