All About String in JavaScript
Table of contents
- Declaration of String
- charAt()-
- String.length-
- charCodeAt():
- concat():
- indexOf():
- lastIndexOf():
- localeCompare():
- match():
- replace():
- search():
- slice():
- split():
- substring():
- toLowerCase():
- toUpperCase():
- trim():
- valueOf():
- padStart() :
- padEnd() :
- codePointAt() :
- fromCodePoint():
- repeat() :
- includes():
- endsWith() :
- startsWith():
- matchAll():
- normalize():
- localeCompare():
- toLocaleLowerCase():
- toLocaleUpperCase():
- toLocaleString() :
- toJSON():
- toLocaleID():
- toLocaleData():
Strings are a powerful data type in JavaScript. They can be used to store text, and they can be manipulated using a variety of methods. The methods described Below are just a few of the many string operations that are available in JavaScript.
in JavaScript, there are multiple Operations we can do on String Here below I am sharing a few of the important Operations with examples and Explanation Also
Declaration of String
in JavaScript, we can form two types to declare a String
// String Declare in JavaScript
// Type - 1
const name1 = "ravindra"
// Type - 2 ( Strings objects )
const name2 = new String('ravindra');
Do not create Strings objects. The new
keyword complicates the code and slows down execution speed.
charAt()-
The charAt() function returns the character at a specified index in a string. If the index is out of range, charAt() returns an empty string.
console.log(name1.charAt(2)); // 2
String.length-
the length keyword in JavaScript gives the length of a string. It is a property of the String object and can be used to get the number of characters in a string. The length property is read-only, so it cannot be changed.
Syntax-
let name1 = "ravindra"
console.log(name1.length) // 8
charCodeAt():
Returns the Unicode code point of the character at a specified index in a string.
var str = "Hello, world!"; var codePoint = str.charCodeAt(0); // 72
concat():
Concatenates two or more strings together.
var str1 = "ravindra"; var str2 = ", sirvi!"; var str3 = str1.concat(str2); // "ravindra, sirvi!"
indexOf():
Returns the index of the first occurrence of a substring in a string.
Code snippet
var str = "Hello, world!"; var index = str.indexOf("world"); // 6
lastIndexOf():
Returns the index of the last occurrence of a substring in a string.
var str = "Hello, sirvi!";
var index = str.lastIndexOf("!"); // 12
localeCompare():
Compares two strings according to the current locale.
var str1 = "Ravindra";
var str2 = "ravindra";
var result = str1.localeCompare(str2); // -1
match():
Returns an array of matches for a regular expression in a string.
var str = "Hello, world!";
var regex = /[a-z]/;
var matches = str.match(regex); // ["h", "e", "l", "l", "o"]
replace():
Replaces all occurrences of a substring in a string with another string.
var str = "ravindra, sirvi!";
var newStr = str.replace("sirvi", "universe"); // "Hello, universe!"
search():
Returns the index of the first occurrence of a substring in a string, or -1 if the substring is not found.
var str = "Hello, world!";
var index = str.search("world"); // 6
slice():
Returns a new string that is a substring of the original string.
var str = "Hello, world!";
var newStr = str.slice(0, 5); // "Hello"
split():
Splits a string into an array of substrings, using a regular expression or a string as a delimiter.
var str = "Hello, world!";
var arr = str.split(","); // ["Hello", "world!"]
substring():
Returns a new string that is a substring of the original string, starting at a specified index and ending at a specified index.
var str = "Hello, world!";
var newStr = str.substring(0, 5); // "Hello"
toLowerCase():
Converts all the characters in a string to lowercase.
var str = "Hello, world!";
var newStr = str.toLowerCase(); // "hello, world!"
toUpperCase():
Converts all the characters in a string to uppercase.
var str = "Hello, world!";
var newStr = str.toUpperCase(); // "HELLO, WORLD!"
trim():
Removes whitespace from the beginning and end of a string.
var str = " Hello, world! ";
var newStr = str.trim(); // "Hello, world!"
valueOf():
Returns the value of a string as a primitive value.
var str = "123";
var num = str.valueOf(); // 123
padStart() :
Returns a new string that is the same as the original string, but padded with a specified number of characters on the left.
var str = "Hello";
var newStr = str.padStart(10, " "); // " Hello"
padEnd() :
Returns a new string that is the same as the original string, but padded with a specified number of characters on the right.
var str = "Hello";
var newStr = str.padEnd(10, " "); // "Hello "
codePointAt() :
Returns the Unicode code point of the character at a specified index in a string.
var str = "Hello";
var codePoint = str.codePointAt(0); // 72
fromCodePoint():
Returns a string that is the concatenation of the characters represented by a specified array of Unicode code points.
var codePoints = [72, 101, 108, 108, 111];
var str = String.fromCodePoint(...codePoints); // "Hello"
repeat() :
Returns a new string that is the concatenation of the original string repeated a specified number of times.
var str = "Hello";
var newStr = str.repeat(3); // "HelloHelloHello"
includes():
Returns a boolean value indicating whether the original string contains a specified substring.
var str = "Hello, world!";
var includes = str.includes("world"); // true
endsWith() :
Returns a boolean value indicating whether the original string ends with a specified substring.
var str = "Hello, world!";
var endsWith = str.endsWith("!"); // true
startsWith():
Returns a boolean value indicating whether the original string starts with a specified substring.
var str = "Hello, world!";
var startsWith = str.startsWith("Hello"); // true
matchAll():
Returns an array of all the matches for a regular expression in a string.
var str = "Hello, world!";
var regex = /[a-z]/;
var matches = str.matchAll(regex); // [["h", "e", "l", "l", "o", "w", "r", "d"]]
normalize():
Returns a new string that is the normalized form of the original string.
var str = "Hello, world!";
var newStr = str.normalize(); // "hello, world!"
localeCompare():
Compares two strings according to the current locale, returning a value indicating whether the first string is less than, equal to, or greater than the second string.
var str1 = "Hello";
var str2 = "hello";
var result = str1.localeCompare(str2); // -1
toLocaleLowerCase():
Converts all the characters in a string to lowercase, according to the current locale.
var str = "Hello";
var newStr = str.toLocaleLowerCase(); // "hello"
toLocaleUpperCase():
Converts all the characters in a string to uppercase, according to the current locale.
var str = "Hello";
var newStr = str.toLocaleUpperCase(); // "HELLO"
toLocaleString() :
Converts a string to a string representation that is appropriate for the current locale.
var str = "Hello";
var newStr = str.toLocaleString(); // "Hello"
toJSON():
Converts a string to a JSON string representation.
var str = "Hello";
var newStr = str.toJSON(); // "\"Hello\""
toLocaleID():
Returns the locale identifier of the current locale.
var localeID = navigator.language; // "en-US"
toLocaleData():
Returns the locale data of the current locale.
var localeData = Intl.DateTimeFormat().resolvedOptions().locale; // "en-US"