Home Blog String Methods In Javascript
String Methods In Javascript

String Methods In Javascript

A Javascript string is a sequence of characters enclosed within (‘ ’) or double (“ “) quotation marks. It could be a word, a sentence or even an empty string. Strings are one of the fundamental data types in Javascript and are used extensively for storing and manipulating text data.


A Javascript string stores a series of character like as ‘Hello Smith’ or “Hello Smith” 

let string = 'Hello Smith'
String.length;  =>  // 11


1=> The charAt() method returns the character at a specified index (position) in a string. The index of the first character is 0, the second 1, third 2,...

example:=>   string.charAt(6);   =>  // S


2=> The endsWith() method returns true if a string ends with a specified string.

Otherwise it returns false.

The endsWith() method is case sensitive.
example:=>   string.endsWith('h');   =>  // true


3=> The startsWith() method returns true if a string starts with a specified string.

Otherwise it returns false.

The startsWith() method is case sensitive.
example:=>   string.startsWith('Hello');   =>  // true


4=> The toLowerCase() method converts a string to lowercase letters.

The toLowerCase() method does not change the original string.

example:=>   string.toLowerCase();   =>  // hello smith


5=> The toUpperCase() method converts a string to uppercase letters.

The toUpperCase() method does not change the original string.

example:=>   string.toUpperCase();   =>  // HELLO SMITH


6=>The includes() method returns true if a string contains a specified string.

Otherwise it returns false.

example:=>   string.includes('Hello');   =>  // true


7=> The includes() method is case sensitive

example:=>   string.includes('hello');   =>  // false


8=> The indexOf() method returns the position of the first occurrence of a value in a string.

The indexOf() method returns -1 if the value is not found.

The indexOf() method is case sensitive.

example:=>   string.indexOf('m');    =>  // 7

example:=>   string.indexOf('Hello');    =>  // 0


9=> The repeat() method returns a string with a number of copies of a string.

The repeat() method returns a new string.

The repeat() method does not change the original string.

example:=>   string.repeat(2);   =>  // Hello SmithHello Smith


10=> The replace() method searches a string for a value or a regular expression.

The replace() method returns a new string with the value(s) replaced.

The replace() method does not change the original string.

example:=>   string.replace('Smith','Piyush');   =>  // Hello Piyush


11=> The slice() method returns selected elements in an array, as a new array.

The slice() method selects from a given start, up to a (not inclusive) given end.

The slice() method does not change the original array.

example:=>   string.slice(6,10); =>  // Smit


12=> The split() method splits a string into an array of substrings.

The split() method returns the new array.

The split() method does not change the original string.

If (" ") is used as separator, the string is split between words.

example:=>   string.split(' ');  =>  // [ 'Hello', 'Smith' ]

example:=>   string.split('');   =>  // ['H', 'e', 'l', 'l','o', ' ', 'S', 'm','i', 't','h']


13=> The startsWith() method returns true if a string starts with a specified string.

Otherwise it returns false.

The startsWith() method is case sensitive.

example:=>   string.startsWith('Hello'); =>  // true


14=> The substring() method extracts characters, between two indices (positions), from a string, and returns the substring.

The substring() method extracts characters from start to end (exclusive).

The substring() method does not change the original string.

If start is greater than end, arguments are swapped: (4, 1) = (1, 4).

Start or end values less than 0, are treated as 0.

example:=>   string.substring(6,11); =>  // Smith


15=> The substr() method extracts a part of a string.

The substr() method begins at a specified position, and returns a specified number of characters.

The substr() method does not change the original string.

To extract characters from the end of the string, use a negative start position.

example:=>   string.substr(1,4); =>  // ello


Conclusion


These methods allow developers to manipulate and extract information from strings,

Making Javascript a versatile language for working with textual data.


 Subscribe our newsletter today to stay update.


Explore our other JavaScript related Blogs:-

8 Key ES6 Features In JavaScript

Handling Asynchronous Operation in JavaScript

React vs Vue: Comparison Guide of Leading JavaScript Frameworks

Get in touch wit​h us!

Protected by reCAPTCHA, Privacy Policy & Terms of Service apply.
Leave a Comment

Your email address will not be published.

Submit
Your comment is under review by our moderation team.