How To Get The First 2 Characters Of A String In JavaScript

get the first 2 characters of a string in JavaScript

In this article, I will help you understand Specify two methods, slice(), substr(), and use them to get the first 2 characters of a string in JavaScript. Read on it now.

Get The First 2 Characters Of A String In JavaScript

Method 1: Use slice() method

The slice() method is used to split the string, and the newly split string is a new string with a length less than the original string, which is part of the original string.

Syntax

slice(start, end)

Parameters – Description

  • start: This is the position of the starting character you want to split
  • end: This is the end position (do not take the end position)

Example:

var myString = "slice in javascript";
var newString = myString.slice(0,2); 
console.log(newString);

Output:

sl

We see in javascript the string is counted starting from 0 one by one, so in the example, to get the first two characters of the myString string, we pass in the starting position 0 and the ending position 2.

If you only pass one parameter, the program will consider that as the starting position and separate all remaining characters into a new string.

Example:

var myString = "slice in javascript";
var newString = myString.slice(9); 
console.log(newString);

Output:

javascript

If the parameter you pass in is a negative number, the position you pass in will be counted from the end of the string.

Example:

var myString = "slice in javascript";
var newString = myString.slice(-19, -14); 
var newString2 = myString.slice(-13); 
console.log(newString);
console.log(newString2);

Output:

slice
in javascript

Method 2: Use substr() method

The substr() method is also used to get the substring and is similar to slice(), but the second parameter of substr() does not enter the position of the last character but the size of the string you want to split.

Syntax

substr(start, length)

Parameters – Description

  • start: This is the position of the starting character you want to split
  • length: This is the size of the substring you want to split

Example:

var myString = "substr in javascript";
var newString2 = myString.substr(0,2); 
console.log(newString2);

Ouput:

su

For this example, I get the first two values ​​of the string using the substr() method with two parameters starting as 0 and length as 2.

Like slice(), when we pass a parameter, the program will understand it as the starting position and output the string from the position of the parameter passed to the end of the original string. Below is an example:

var myString = "substr in javascript";
var newString2 = myString.substr(7); 
console.log(newString2);

Output:

in javascript

For substr(), we can pass the first parameter as a negative number, and then the program will count the string starting from the end of the string. Here is an example:

var myString = "substr in javascript";
var newString2 = myString.substr(-10); 
console.log(newString2);

Output:

javascript

Summary

In this tutorial, I showed several ways to get the first 2 characters of a string in JavaScript. Follow and follow the tutorial to understand and use the above methods.

Maybe you are interested:

Leave a Reply

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