String
Description:
Strings are useful for holding data that can be represented in text form. Some of the most-used operations on strings are to check their length, to build and concatenate them using the + and += string operators, checking for the existence or location of substrings with the indexOf() method, or extracting substrings with the substring() method.
Creating strings:
Strings can be created as primitives, from string literals, or as objects, using the String() constructor:
const string1 = "A string primitive";
const string2 = new String("A String object");
Comparing strings:
In JavaScript, you just use the less-than and greater-than operators:
let a = 'a'
let b = 'b'
if (a < b) { // true
// do something
} else if (a > b) {
// do something else
} else {
// otherwise
}
Note that a == b compares the strings in a and b for being equal in the usual case-sensitive way.
Long literal strings:
Sometimes, your code will include strings which are very long. Rather than having lines that go on endlessly, or wrap at the whim of your editor, you may wish to specifically break the string into multiple lines in the source code without affecting the actual string contents. There are two ways you can do this.
Method 1
You can use the + operator to append multiple strings together, like this:
let longString = "This is a very long string which needs " +
"to wrap across multiple lines because " +
"otherwise my code is unreadable."
Method 2
You can use the backslash character (\) at the end of each line to indicate that the string will continue on the next line. Make sure there is no space or any other character after the backslash (except for a line break), or as an indent; otherwise it will not work.
That form looks like this:
let longString = "This is a very long string which needs \
to wrap across multiple lines because \
otherwise my code is unreadable."
Both of the above methods result in identical strings.
JavaScript Type Conversion:
JavaScript variables can be converted to a new variable and another data type:
1- By the use of a JavaScript function
2- Automatically by JavaScript itself
Converting Strings to Numbers:
The global method Number() can convert strings to numbers. Strings containing numbers (like "3.14") convert to numbers (like 3.14). Empty strings convert to 0. Anything else converts to NaN (Not a Number).
Number("3.14") // return 3.14
Number(" ") // return 0
Number("") // return 0
Number("99 88") // return NaN
Converting Numbers to Strings:
The global method String() can convert numbers to strings. It can be used on any type of numbers, literals, variables, or expressions:
String(x) // returns a string from a number variable x
String(123) // returns a string from a number literal 123
String(100 + 23) // returns a string from a number from an expression
The Number method toString() does the same.
x.toString()
(123).toString()
(100 + 23).toString()
String Methods:
|
Method
|
Description
|
|
charAt()
|
Returns the character at the specified index (position)
|
|
charCodeAt()
|
Returns the Unicode of the character at the specified index
|
|
concat()
|
Joins two or more strings, and returns a new joined strings
|
|
endsWith()
|
Checks whether a string ends with specified string/characters
|
|
fromCharCode()
|
Converts Unicode values to characters
|
|
includes()
|
Returns x, rounded upwards to the nearest integer
|
|
indexOf()
|
Returns the position of the first found occurrence of a specified value in a string
|
|
lastIndexOf()
|
Returns the position of the last found occurrence of a specified value in a string
|
|
localeCompare()
|
Compares two strings in the current locale
|
|
match()
|
Searches a string for a match against a regular expression, and returns the matches
|
|
repeat()
|
Returns a new string with a specified number of copies of an existing string
|
|
replace()
|
Searches a string for a specified value, or a regular expression, and returns a new string where the specified values are replaced
|
|
search()
|
Searches a string for a specified value, or regular expression, and returns the position of the match
|
|
slice()
|
Extracts a part of a string and returns a new string
|
|
split()
|
Splits a string into an array of substrings
|
|
startsWith()
|
Checks whether a string begins with specified characters
|
|
substr()
|
Extracts the characters from a string, beginning at a specified start position, and through the specified number of character
|
|
substring()
|
Extracts the characters from a string, between two specified indices
|
|
toLocaleLowerCase()
|
Converts a string to lowercase letters, according to the host's locale
|
|
toLocaleUpperCase()
|
Converts a string to uppercase letters, according to the host's locale
|
|
toLowerCase()
|
Converts a string to lowercase letters
|
|
toString()
|
Returns the value of a String object
|
|
toUpperCase()
|
Converts a string to uppercase letters
|
|
trim()
|
Removes whitespace from both ends of a string
|
|
valueOf()
|
Returns the primitive value of a String object
|