String Methods
Resources
Javascript strings are primitive and immutable: All string methods produce a new string without altering the original string.
1. String()
function
The String()
function converts its argument to a string.
const myNum2 = 123;
const myString2 = String(myNum2);
console.log(typeof myString2); // Output: string
2. String Length Method
The length
property returns the length of a string:
let text = "Hello";
let length = text.length;
console.log(length); // Output: 5
3. Methods for Extracting String Character
These 4 methods for extracting string characters:
- The
charAt(_position_)
Method - The
charCodeAt(_position_)
Method - The
at(_position_)
Method - Using property access
[]
like in arrays.
charAt()
: the method returns the character at a specified index (position) in a string.
let text = "HELLO WORLD";
let char = text.charAt(0);
console.log(char); // Output: "H"
charCodeAt()
: the method returns the code of the character at a specified index in a string. The method returns a UTF-16 code (an integer between 0 and 65535).
let text = "HELLO WORLD";
let char = text.charCodeAt(0); // Output: 72
at()
: the method returns the character at a specified index (position) in a string.
- The
at()
method is a new addition to JavaScript. It allows the use of negative indexes whilecharAt()
does not.
const name = "University";
let letter = name.at(2); // Output: i
// negative index
let char = name.at(-1); // Output: y
- Property Access
[]
- If no character is found,
[]
returns undefined, whilecharAt()
returns an empty string. - It is read-only.
str[0] = "A"
gives no error (but does not work!)
let text = "HELLO WORLD";
let char = text[0]; // Output: H
console.log(text[-1]); // Output: undefined
text[0] = "A"; // Gives no error, but does not work, because JS is immutable
4. Methods for Extracting String Parts
There are 3 methods for extracting a part of a string:
slice(_start_, _end_)
substring(_start_, _end_)
substr(_start_, _length_)
slice()
: extracts a part of a string and returns the extracted part in a new string.
- The method can take 2 parameters: start position, and end position (end not included).
- The method can take only 1 parameter, which is the start position, the method will slice the rest of the string starting from this starting position.
- If the parameter value is negative, the position is counted from the end of the string.
let text = "Apple, Banana, Kiwi";
// two parameters
let part1 = text.slice(7, 13);
console.log(part1); // Output: Banana (the character at index 13 is not included)
// one parameter
let part2 = text.slice(7);
console.log(part2);
// negative parameter
let part3 = text.slice(-12, -6)
console.log(part3); // Output: Banana
substring()
:substring()
is similar toslice()
.
- The difference is that start and end values less than 0 (negative) are treated as 0 in
substring()
. - If you omit the second parameter,
substring()
will slice out the rest of the string.
let str = "Apple, Banana, Kiwi";
let part = str.substring(7, 13); // Output: Banana
// negative parameter
console.log(str.substring(-10, 13)) // Apple, Banana
// one parameter
console.log(str.substring(7)) // Output: Banana, Kiwi
substr()
:substr()
is similar toslice()
.
- The difference is that the second parameter specifies the length of the extracted part in the second argument of the parameter.
- If you omit the second parameter,
substr()
will slice out the rest of the string. - If the first parameter is negative, the position counts from the end of the string.
let str = "Apple, Banana, Kiwi";
let part = str.substr(7, 6);
// one parameter
console.log(str.substr(7)); // Output: Banana, Kiwi
// negative parameter
console.log(str.substr(-4)); // Output: Kiwi
5. Converting to Upper and Lower Case
- A string is converted to upper case with
toUpperCase()
. - A string is converted to lower case with
toLowerCase()
.
let text = "Hello World!";
let upper = text.toUpperCase(); // Output: HELLO WORLD!
let lower = text.toLowerCase(); // Output: hello world!
6. concat()
method
The concat()
method joins two or more strings.
- The
concat()
method can be used instead of the+
operator to concatenate string.
// these two lines do the same thing. // Output: Hello World!
text = "Hello" + " " + "World!";
text = "Hello".concat(" ", "World!");
7. trim
method
The trim()
method removes whitespace from both sides of a string:
let text1 = " Hello World! ";
let text2 = text1.trim(); // Output: "Hello World"
- The
trimStart()
method works liketrim()
, but removes whitespace only from the start of a string. - The
trimEnd()
method works liketrim()
, but removes whitespace only from the end of a string.
let text1 = " Hello World! ";
let text2 = text1.trimStart(); // Output: "Hello World! "
let text3 = text1.trimEnd(); // Output: " Hello World!"
8. repeat()
method
The repeat()
method returns a string with a number of copies of a string. The method returns a new string and does not change the original string. The syntax is: string_.repeat(_count_)
let text = "Hello world!";
let result = text.repeat(2); // Output: Hello world!Hello world!
9. replace()
method
The replace()
method replaces a specified value with another value in a string. The method does not change the string it is called on, it returns a new string.
- By default,
replace()
method replaces only the first match. - By default,
replace()
method is case sensitive.
let oldText = "Please visit Microsoft and Microsoft!";
let newText = oldText.replace("Microsoft", "W3Schools");
// Output: Please visit W3Schools and Microsoft!
10. split()
method
A string can be converted to an array with the split()
method. The method takes in an argument that is the separator:
text.split(",") // Split on commas
text.split(" ") // Split on spaces
text.split("|") // Split on pipe
text.split("") // Split on each character
let myString = "Hello!";
let myArray = myString.split("");