String Methods: includes(), replace(), slice(), split(), concat(), toUpperCase(),toLowerCase(),trim().

Shongkor Talukdar
2 min readMay 8, 2021

includes():

ES6 introduced this method to determine whether the specified sub-string exists in given String. It matches the String case-sensitively.

The syntax of the includes() method is: str.includes(searchString, position)

var str = “medium blog.”;
var n = str.includes(“blog”);
console.log(n) // the output is ‘true’

replace():

the replace() will only replace the first occurrence of the specified character. To replace all the occurrence you can use the global (g) modifier. The following example will show you how to replace all underscore (_) character in a string with hyphen (-).

var str = “Hello World.”;
var res = str.replace(“World”, “medium”);
console.log(res) // Hello medium.

slice():

To get a portion of a string use this method with the starting index and the ending index (optional) as input parameters. The result will be a new string starting from the start parameter to the end parameter (or to the end of the string if end is not specified).

var str = "Hello world!";
var res = str.slice(6, 8);
console.log(res); // wo
var res = str.slice(4);
console.log(res); // o world!
var res = str.slice(-5);
console.log(res); // ld!

split():

the split() method in JavaScript splits the string into the array of substrings, puts these substrings into an array, and returns the new array. It does not change the original string.

string = “A-quick-brown-fox”array_of_strings = string.split(“-”)console.log(array_of_strings)//[ 'A', 'quick', 'brown', 'fox' ]

concat():

The concat() function concatenates the string arguments to the calling string and returns a new string. Changes to the original string or the returned string don't affect the other.

const name1 = “Shongkor”;
const name2 = “Talukdar”;
const fullName = name1.concat(name2);
console.log(fullName);//ShongkorTalukdar

toUpperCase():

The toUpperCase() method returns a string converted to uppercase.

var totn_string = 'TechOnTheNet';

console.log(totn_string.toUpperCase())//TECHONTHENET;

toLowerCase():

The toUpperCase() method returns a string converted to lowercase.

const string2 = “HELLO WORLD!”
console.log(string2.toLowerCase())//hello world

trim():

Trim() method use to remove the unnessecer white space from the string.

const string = “ remove white space ”;
console.log(string.trim())// remove white space

--

--