10 Javascript String.

Sudipto Acharjee
3 min readMay 5, 2021
  1. String.concat;
    String.concat is used to add two string and get one .

Example:
const string1= ‘Hello’;
const string2=’world’;

console.log(string1.concat(‘ ‘,string2));
//Output : “Hello world”

console.log(string2.concat(‘ ‘, string1));
//Output : “world hello”

2) String.charAt()
String.charAt() used to find out specific position of a character from the string.

Example:
const sentence1 = “Hello world”;
const index1= 4;
console.log(`The character at index ${index1} is ${sentence1.charAt(index1)}`)
//Output : “The character at index 4 is o”

3)String.includes();
String.includes() is used to find out one string within another string and returning true and false.

Example: const sentence = ‘Hello World.’;

const word = ‘Hello’;

console.log(`The word “${word}” ${sentence.includes(word) ? ‘is’ : ‘is not’} in the sentence`);
//Output: “The word “Hello” is in the sentence”.

4)String.endsWith()
String.endsWith() is used to find out string last character position from a another string .
Example: const str1 = ‘Cats are the best!’;
console.log(str1.endsWith(‘best’,17));
// Output: true

5)String.indexOf()
String.indexOf() is used to find out a word position from a string .

Example:
const paragraph = ‘The quick brown fox jumps over the lazy dog. If the dog barked, was it really lazy?’;

const searchTerm = ‘dog’;
const indexOfFirst = paragraph.indexOf(searchTerm);

console.log(`The index of the first “${searchTerm}” from the beginning is ${indexOfFirst}`);
// output: “The index of the first “dog” from the beginning is 40"

6)String.lastindexof()
String.lastindexof used to find out a word position from a string .

Example:

const paragraph = ‘Hello world’;
const searchTerm = ‘world’;
console.log(`The index of the first “${searchTerm}” from the end is ${paragraph.lastIndexOf(searchTerm)}`);

//Output: The index of the first “world” from the end is 6

7.String.replace()
String. replace() is used to replace a word of a string.

Example: const word=” I have a cat”
const now=word.replace (‘cat’,’ dog’);
console.log(now)
//Output: I have a dog.
Here we find ‘dog’ instead of cat .because word. replace() change the previous value and show output after change the value.

8.String.slice();
String. slice() is used to extract a section of a string base on a fixed range of value and return it.
Example:
const str = ‘My name is Shakib Al Hasan’;
console.log(str.slice(10));
//Output: Shakib Al Hasan

str. slice(10) defines from the string, to slice another value after 10th number position. After the 10th position rest value, we can see it as Output. If we input the negative value then it's working on the reverse side.

9.String.split()
String.split() use to devide a string into substring and return the value as a array.
Example: const str = ‘Tomorrow i will be going to meet with Elon mask’;

const words = str.split(‘ ‘);
console.log(words[3]);
// Output: “be”;

str.split(3) work ,firstly it’s find out the value of 3rd position and split it.Here 3rd position value is ‘be’ .so Expected value is ‘be’.

10. String.startsWith()
String.startsWith() works, where a string begins with the character of a specified string. we can check the value of the string within the fixed position, is true or false.
Example:
const str1 = ‘Cristiano Ronaldo’;

console.log(str1.startsWith(‘Cris’));
// Output: true

const str1 = ‘Cristiano Ronaldo’;

console.log(str1.startsWith(‘Cris’,10));
//Output: false.

In the first example, we can clearly objerb that the “Cris” word is found from ‘Cristiano”.So the result is true. But the Second example ‘Cris’ word is not in the 10th position of the string. So, the return of the programme is false.

--

--