String:
String is basically a sequence of characters that is used for storing and managing text data. JavaScript String is primitive data types and are immutable(means you cannot change the original string).There are two ways to create a string.
Using String Literals
let name = "Hello World";
here you have to enclosed the text inside the double or single quotes.
Using String Object
let name = new String("Hello World");
String Methods :
charAt Method: returns the character from a specified index.
let name = "Hello World";
let value = name.charAt(1);
console.log(value);
//value = e
concat method: Join two or more string together
let str = "Hello ";
let str1 = "World";
let total = str.concat(str1);
console.log(total);
//total = Hello World
replace method: replace the given string with specified replacement values
let str = "Your name";
let replaceValue = str.replace("Your", "My");
console.log(replaceValue);
//My name
search method: It searches a string against a specified values or regular expressions
let text = "Please keep quiet!";
console.log(text.search("keep"));
console.log(text.search(/quiet/));
// 7
//12
note: it will return the first occurrence of the string.
indexOf method: It return the index of first occurrence of the string. It cannot take regular expression values.
let text = "Move ahead!";
let index = text.indexOf("ahead");
console.log(index);
//5
split method: convert a string into array
let text = "Move ahead!";
let value = text.split(" ");
console.log(value);
//[ 'Move', 'ahead!' ]
slice method: extracts a part of a string and returns the extracted part in a new string.
it takes start position and end position(not included) in parameter.
let text = "Move ahead!";
let value = text.slice(6, 10);
console.log(value);
//head
Hope you find this helpful
Top comments (0)