A string in javascript stores a series of characters, for example:
"Harry Potter"
A sting can be anything inside a single or double quote.
Today we will discuss some commonly used string prototypes of javascript.
concat()
The concat() method of string contacts or add one string to the other and returns a new string. This addition or concatenation of string is like the second string that sits after the first one and makes a chain-like structure.
Example:
const str1 = “Hello”
const str2 = “Albus”
console.log(str1.concat(', ', str2));
Output: “Hello, Albus”
indexOf()
The indexOf() method returns exactly where the string belongs. By default, the indexing or identity of the position of a string starts from [0] and then moves on [1,2,3, ….]. So, if we want to know the exact position of a word in a string, we need to use the indexOf() method. One important thing here to note that, if we ask for any invalid index or a string or word that does not exist in particular that string, it will return the value -1.
Example:
const paragraph = “I am reading about javascript”;
const searchString = “about”;
const index = paragraph.indexOf(searchString );
console.log(index)
Output: 13
replace()
replace() method returns a string. If we have a string with series of words of multiple characters, we can change or replace a string or character of the mother string using the replace() method.
Example:
const paragraph = “I am reading about javascript”;
console.log(paragraph.replace('reading', ‘writing’));
Output: I am writing about javascript
toLowerCase()
If we want to make a string all lowercase, we will use the toLowerCase() method.
Example:
const dotted = "Bangladesh";
console.log(dotted.toLowerCase());
Output:
bangladesh
toUpperCase()
The toUpperCase() method returns a string that all the characters are converted to uppercase. This is exactly the opposite of the toLowerCase() method.
Example:
const lower = "hello";
console.log(lower.toUpperCase());
Output:
HELLO
charAt()
Previously we have discussed finding words inside of a string using their index. Now we will discuss something that closely matches that. We will try to find a character in a string using the charAt() method.
Example :
```const text = "Hello World";
const index = 4;
Output:
o
## includes()
The includes() method performs a case-sensitive search.It finds whether one string may be found within another string. And returns true or false as output.
Example:
const isThere = "hello world";
const word = "word";
console.log(isThere.includes(word));
Output:
false
## slice()
The slice() method extracts a section of a string and returns it as a new string, without modifying the original string.
Example:
const text = 'The quick brown fox jumps over the lazy dog.';
console.log(str.slice(31));
Output: "the lazy dog."
If we want to slice from one index and end another,we have to write that.
const text = 'The quick brown fox jumps over the lazy dog.';
console.log(str.slice(4, 19));
Output: "quick brown fox"
We can determine inverse index too.
const text = 'The quick brown fox jumps over the lazy dog.';
console.log(str.slice(-4));
Output: "dog."
## split()
The split() method divides a String into an ordered list of substrings, puts these substrings into an array, and returns the array.
const str = 'The quick brown fox jumps over the lazy dog.';
const words = str.split(' ');
console.log(words[3]);
Output: "fox"
## startsWith()
The startsWith() method determines whether a string begins with the characters of a specified string, returning true or false as appropriate.
Example :
const str1 = 'Saturday night plans';
console.log(str1.startsWith('Sat');
Output: true
Top comments (0)