JavaScript – String Methods By Example

Today we will discuss the most useful javascript string methods. Javascript string methods help you to work with strings.

We will discuss javascript methods like javascript string replace, javascript string contains, javascript string length, javascript string concatenation, javascript string substring, remove character from string javascript, javascript string uppercase, javascript string lowercase,
javascript string substr.

We need to know some of the most important and useful javascript string methods.

Let’s see JavaScript String Methods

The List of String Methods in JavaScript

  • String length
  • Converting to Lower Case
  • Converting to Upper Case
  • Finding a String
  • Search String Method
  • The slice() Method
  • The substring() Method
  • The substr() Method
  • Replacing String Content
  • JavaScript String includes() Method
  • String concat() Method
  • Javascript charAt() Method

String length

It is used to count the number of characters in a string.

Example

var lng = "Hello Tutsmake";
var x = lng.length;
Output 

// return
14

Converting to Lower Case

The javascript toLocaleLowerCase() is used to changed string into lower case.

Example

var str = "Hello Dev!";
var res = str.toLocaleLowerCase();
Output
// return
hello dev!

Converting to Upper Case

The javascript toLocaleUpperCase () is used to changed string into upper case.

Example

var str = "Hello Dev!";
var res = str.toLocaleUpperCase();
Output
// return
HELLO DEV!

Finding a String

The indexoff () method returns the first position of a specified value in a string.

Example

 var txt = "Lets find where 'pen' occurs!";
 var test = txt.indexOf("pen");
Output

// return
17

Search String Method

The javascript search () method searches a string for the specified value, and returns the status of the match.

Example

var str = "hello dev!"; 
var n = str.search("dev");
Output
//Return
6

Slice String Method

The slice () method removes the parts of a string and returns the extracted parts to a new string.

Use the Start and End Ultimate to specify the part of the string that you want to remove.

Example

var str = "Developers world!"; 
var res = str.slice(0, 10);
Output
// return
Developers

The substring() Method

The Javascript substring() method is used to removes the characters from one string, between two specified indices, and returns the new sub string.

Example

  var str = "Hello dev!";
  var res = str.substring(1, 4);
Output
//return
ell

The substr() Method

Parts of a string string () method begins on the character in the specified position, and returns the specified number of characters.

Example

 var str = "Hello dev!";
 var res = str.substring(1, 4);
Output
//return
ello

The Replacing() Method

The Javascript replace() changes the defined value to an another value:

Example

var str = "Hello Dev!";
var res = str.replace("Dev", "World");
Output
// return
Hello World

JavaScript String includes() Method

The includes() method is used to determine whether a string contains the characters of a specified string or not. If is exist return true or not return false.

Example

var str = "Hello world, my name is tutsmake.";
var n = str.includes("name");
Output
// return
True

More Examples of javascript Include() function

String concat() Method

The concat() method is used for join two or more strings.

Example

var str1 = "Hello ";
var str2 = "keny!";
var res = str1.concat(str2);
Output
// return
Hello Keny

The charAt() Method

The Javascript charAt() is used to take a character to a described index location:

Example

var txt = "Hello Keny";
txt.charAt(0);
Output
// return
H

Be First to Comment

Leave a Reply

Your email address will not be published. Required fields are marked *