Javascript String - 25 Methods and 1 Property (Reference)
In Javascript, strings are objects. Basically, strings are used to store text. It has methods and properties that can be used to manipulate or to get information about the string.
String Methods:
String Properties:
concat()
The concat() method is used to join two or more strings.
var str1 = "Hello ";
var str2 = "world!";
var x = str1.concat(str2);
// Hello world!
// str.concat(str1, str2, ..., strX)
repeat()
The repeat() method is used to repeat a string.
var str = "Hello world!";
var x = str.repeat();
// Hello world!Hello world!
// str.repeat(count)
slice()
The slice() method is used to extract a part of a string and return a new string.
var str = "Hello world!";
var x = str.slice(0, 5);
// Hello
// str.slice(start, end)
replace()
The replace() method is used to replace a part of a string with another string.
var str = "Hello world!";
var x = str.replace("Hello", "Hi");
// Hi world!
// str.replace(searchvalue, newvalue)
charAt()
The charAt() method is used to return the character at the specified index.
var str = "HELLO WORLD";
var x = str.charAt(0);
// H
// str.charAt(index)
charCodeAt()
The charCodeAt() method is used to return the Unicode of the character at the specified index.
var str = "HELLO WORLD";
var n = str.charCodeAt(0);
// 72
// str.charCodeAt(index)
endsWith()
The endsWith() method is used to check whether a string ends with the specified string.
var str = "Hello world!";
var x = str.endsWith("d!");
// true
var str = "Hello world!";
var x = str.endsWith("d!", 3);
// false
// str.endsWith(searchvalue, length)
startsWith()
The startsWith() method is used to check whether a string starts with the specified string.
var str = "Hello world!";
var x = str.startsWith("He");
// true
var str = "Hello world!";
var x = str.startsWith("d!", 3);
// false
// str.startsWith(searchvalue, length)
indexOf()
The indexOf() method is used to find the position of the first occurrence of a specified value in a string.
var str = "Hello world!";
var x = str.indexOf("world");
// 6
var str = "Hello world!";
var x = str.indexOf("world", 8);
// -1
// str.indexOf(searchvalue, start)
lastIndexOf()
The lastIndexOf() method is used to find the position of the last occurrence of a specified value in a string.
var str = "Hello world!";
var x = str.lastIndexOf("world");
// 6
var str = "Hello world!";
var x = str.lastIndexOf("world", 8);
// 6
var str = "Hello world!";
var x = str.lastIndexOf("world", 4);
// -1
// str.lastIndexOf(searchvalue, start)
includes()
The includes() method is used to check whether a string contains the specified string.
var str = "Hello world!";
var x = str.includes("world");
// true
var str = "Hello world!";
var x = str.includes("world", 7);
// false
// str.includes(searchvalue, start)
search()
The search() method is used to search a string for a specified value and return the position of the match.
var str = "Hello world!";
var x = str.search("world");
// 6
// str.search(searchvalue)
match()
The match() method is used to search a string for a specified value and return an array containing the matches.
var str = "Hello world!";
var x = str.match(/o/g);
// 0,0
var str = "Hello world!";
var x = str.match(/xxx/g);
// null
// str.match(regexp)
split()
The split() method is used to split a string into an array of substrings.
var str = "Hello world!";
var x = str.split(" ");
// ["Hello","world!"]
var str = "Hello world!";
var x = str.split(" ", 2);
// ["Hello","world!"]
// str.split(separator, limit)
substr()
The substr() method is used to extract a part of a string.
var str = "Hello world!";
var x = str.substr(1, 4);
// ello
var str = "Hello world!";
var x = str.substr(2);
// llo world!
// str.substr(start, length)
substring()
The substring() method is used to extract a part of a string.
var str = "Hello world!";
var x = str.substring(1, 4);
// ell
var str = "Hello world!";
var x = str.substring(2);
// llo world!
// str.substring(start, length)
toLowerCase()
The toLowerCase() method is used to convert a string to lower case.
var str = "Hello World!";
var x = str.toLowerCase();
// hello world!
toUpperCase()
The toUpperCase() method is used to convert a string to upper case.
var str = "Hello World!";
var x = str.toUpperCase();
// HELLO WORLD!
toLocaleLowerCase()
The toLocaleLowerCase() method is used to convert a string to lower case, according to the host's locale.
var str = "Hello World!";
var x = str.toLowerCase();
// hello world!
toLocaleUpperCase()
The toLocaleUpperCase() method is used to convert a string to upper case, according to the host's locale.
var str = "Hello World!";
var x = str.toUpperCase();
// HELLO WORLD!
trim()
The trim() method is used to remove whitespace from both ends of a string.
var str = " Hello World! ";
var x = str.trim();
// Hello world!
localeCompare()
The localeCompare() method is used to compare two strings in the current locale.
var str1 = "ab";
var str2 = "cd";
var x = str1.localeCompare(str2);
// -1
var str1 = "cd";
var str2 = "ab";
var x = str1.localeCompare(str2);
// 1
var str1 = "ab";
var str2 = "ab";
var x = str1.localeCompare(str2);
// 0
// compare the two strings in the current locale.
// str.localeCompare(compareString)
// Returns -1 if str1 is sorted before str2
// Returns 0 if the two strings are equal
// Returns 1 if str1 is sorted after str2
fromCharCode()
The fromCharCode() method is used to create a string from the specified sequence of Unicode values.
var x = String.fromCharCode(65);
// A
var x = String.fromCharCode(72, 69, 76, 76, 79);
// HELLO
// String.fromCharCode(n1, n2, ..., nX)
toString()
The toString() method is used to convert a string to a string.
var str = "Hello world!";
var x = str.toString();
// Hello world!
valueOf()
The valueOf() method is used to convert a string to a string.
var str = "Hello world!";
var x = str.valueOf();
// Hello world!
length
The length property is a read-only property that contains the number of characters in a string. It is the only built-in property of the String object.
var str = "Hello world!";
var x = str.length;
// 12
