The String
object provides a variety of methods to work with text. Let’s look at some of the most useful ones.
Manipulation Methods
Method | Description |
---|---|
concat() | Combines two or more strings |
slice() | Extracts a section of a string |
substring() | Returns a part of the string between specified indices |
substr() | Returns a part of the string with a specified length |
split() | Divides the string into an array using a delimiter |
replace() | Replaces a part of the string |
toUpperCase() | Converts all characters to uppercase |
toLowerCase() | Converts all characters to lowercase |
trim() | Removes whitespace from both ends of the string |
concat()
Combines two or more strings and returns a new string.
const str1 = "Hello";
const str2 = "World";
const result = str1.concat(" ", str2);
console.log(result); // "Hello World"
slice()
Extracts a section of a string and returns a new string without modifying the original.
const str = "JavaScript";
const result = str.slice(0, 4);
console.log(result); // "Java"
substring()
Returns a part of the string between specified indices.
const str = "JavaScript";
const result = str.substring(4, 10);
console.log(result); // "Script"
substr()
Similar to substring()
, but the second parameter specifies the length of the substring.
const str = "JavaScript";
const result = str.substr(4, 6);
console.log(result); // "Script"
split()
Divides the string into an array of substrings using a specified delimiter.
const str = "Hello World";
const result = str.split(" ");
console.log(result); // ["Hello", "World"]
replace()
Replaces a part of the string with another string or regular expression.
const str = "Hello World";
const result = str.replace("World", "JavaScript");
console.log(result); // "Hello JavaScript"
toUpperCase()
Converts all characters in the string to uppercase.
const str = "Hello World";
const result = str.toUpperCase();
console.log(result); // "HELLO WORLD"
toLowerCase()
Converts all characters in the string to lowercase.
const str = "Hello World";
const result = str.toLowerCase();
console.log(result); // "hello world"
trim()
Removes whitespace from both ends of the string.
const str = " Hello World ";
const result = str.trim();
console.log(result); // "Hello World"
Search Methods
Method | Description |
---|---|
indexOf() | Returns the index of the first occurrence |
lastIndexOf() | Returns the index of the last occurrence |
includes() | Determines if a substring is present |
startsWith() | Determines if it starts with a string |
endsWith() | Determines if it ends with a string |
indexOf()
Returns the index of the first occurrence of a substring, or -1
if not found.
const str = "Hello World";
const index = str.indexOf("World");
console.log(index); // 6
lastIndexOf()
Returns the index of the last occurrence of a substring, or -1
if not found.
const str = "Hello World World";
const index = str.lastIndexOf("World");
console.log(index); // 12
includes()
Determines if a substring is present within a string, returning true
or false
.
const str = "Hello World";
const result = str.includes("World");
console.log(result); // true
startsWith()
Determines if a string starts with the characters of a substring, returning true
or false
.
const str = "Hello World";
const result = str.startsWith("Hello");
console.log(result); // true
endsWith()
Determines if a string ends with the characters of a substring, returning true
or false
.
const str = "Hello World";
const result = str.endsWith("World");
console.log(result); // true
Comparison Methods
Method | Description |
---|---|
localeCompare() | Compares two strings in a specific locale |
normalize() | Returns the normalized Unicode form of the string |
localeCompare()
Compares two strings in a specific locale and returns a number indicating the comparison.
const str1 = "a";
const str2 = "b";
const result = str1.localeCompare(str2);
console.log(result); // -1 (because "a" is less than "b" in lexicographic order)
normalize()
Returns the normalized Unicode form of the string, useful for comparing Unicode characters.
const str = "\u00F1";
const normalizedStr = str.normalize("NFD");
console.log(normalizedStr); // "ñ"
Conversion Methods
Method | Description |
---|---|
toString() | Converts and returns the string as a text representation. |
valueOf() | Returns the primitive value of a String object. |
toString()
Converts and returns the string as a text representation.
const num = 123;
const str = num.toString();
console.log(str); // "123"
valueOf()
Returns the primitive value of a String
object.
const str = new String("Hello World");
const primitiveValue = str.valueOf();
console.log(primitiveValue); // "Hello World"
Access Methods
Method | Description |
---|---|
charAt() | Returns the character at the specified index |
charCodeAt() | Returns the Unicode value of a character |
codePointAt() | Returns the Unicode value of a character (16 bits) |
charAt()
Returns the character at the specified index.
const str = "Hello World";
const char = str.charAt(6);
console.log(char); // "W"
charCodeAt()
Returns the Unicode value of the character at the specified index.
const str = "Hello World";
const code = str.charCodeAt(6);
console.log(code); // 87 (Unicode code of "W")
codePointAt()
Returns the Unicode value of a character at the specified index, including characters over 16 bits.
const str = "😊";
const codePoint = str.codePointAt(0);
console.log(codePoint); // 128522
Template Methods
Method | Description |
---|---|
padStart() | Pads the beginning until reaching a specified length |
padEnd() | Pads the end until reaching a specified length |
repeat() | Repeats a string a specified number of times |
padStart()
Pads the string at the beginning with another string until reaching a specified length.
const str = "5";
const paddedStr = str.padStart(3, "0");
console.log(paddedStr); // "005"
padEnd()
Pads the string at the end with another string until reaching a specified length.
const str = "5";
const paddedStr = str.padEnd(3, "0");
console.log(paddedStr); // "500"
repeat()
Returns a new string with a specified number of copies of the original string concatenated.
const str = "abc";
const repeatedStr = str.repeat(3);
console.log(repeatedStr); // "abcabcabc"
Regex Methods
Method | Description |
---|---|
match() | Searches for matches between a string and a regular expression |
matchAll() | Returns an iterator with all matches of a regular expression |
match()
Searches for matches between a string and a regular expression, returning an array or null
.
const str = "Hello World";
const result = str.match(/World/);
console.log(result); // ["World"]
matchAll()
Returns an iterator with all matches of a regular expression in the string.
const str = "Hello World World";
const iterator = str.matchAll(/World/g);
console.log([...iterator]); // [["World"], ["World"]]