DEV Community

Md. Akramul Hoque
Md. Akramul Hoque

Posted on

Javascript String Method Concepts

String charAt(position) Method:- The charAt() method accepts the index of specified position and returns the character of this specified position in a string.

Example: 
let a = "This is a string chatAt() method";
let char = a.charAt(0);
console.log(char);

Output: T
Enter fullscreen mode Exit fullscreen mode

String charCodeAt(position) Method:- The charCodeAt() method accepts the index of specified position and returns a number indicating the Unicode value of the character at the given position in a string.

Example: 
let a = "This is a string chatAt() method";
let char = a.charCodeAt(0);
console.log(char);

Output: 84
Enter fullscreen mode Exit fullscreen mode

String concat([string,,]) Method:- The concat() joins two or more strings and returns a new string. This method can be used instead of the plus operator.

Example:
let a = "Javascript";
let b = "String Concept";
let c = a.concat(" ", b);
console.log(c);

Output: Javascript String Concept
Enter fullscreen mode Exit fullscreen mode

String includes() Method:- The includes() method accepts a string and its position and then returns true if a string contains a specified string, Otherwise it returns false. The includes() method is case sensitive. Its syntax is like:

string.includes(searchvalue, start)

Example:
let a = "This is a text string";
let result1 = a.includes("tex", 10);
let result2= a.includes("tex", 5);
console.log(result1);
console.log(result2);

Output: 
True
False

Enter fullscreen mode Exit fullscreen mode

String lastIndexOf() Method:- The lastIndexOf() method accepts a specified string and searches the string from the end to the beginning position. It returns the index from the beginning of this string, otherwise returns -1 if the value is not found. It is also case sensitive. Its syntax is like:

string.lastIndexOf(searchvalue, start)

Example:
let a = "This is a text string";
let result1 = a.lastIndexOf("text");
let result2= a.lastIndexOf("Text");
console.log(result1);
console.log(result2);

Output: 
10
-1

Enter fullscreen mode Exit fullscreen mode

String replace() Method:- The replace() method accepts two specified strings and Search specific string value and replace with specified replace Value string and return new string. Regular expressions can also be used as searchValue. Its syntax is like:

string.replace(searchValue, replaceValue)

Example:
let a = "Please visit my portfolio";
let result= a.replace("portfolio", "website");
console.log(result);

Output: 
Please visit my website
Enter fullscreen mode Exit fullscreen mode

String slice() Method:- The slice() method accepts two indexes as parameters and extracts a section of a string based on specified starting and ending index and returns a new string. Its syntax is like:

string.slice(startIndex, endIndex)

Example:
let a = "Please visit my portfolio";
let result= a.slice(7,26);
console.log(result);

Output: 
visit my portfolio
Enter fullscreen mode Exit fullscreen mode

Top comments (0)