Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript How to get first three characters of a string

This may duplicate with previous topics but I can't find what I really need.

I want to get a first three characters of a string. For example:

var str = '012123'; console.info(str.substring(0,3));  //012 

I want the output of this string '012' but I don't want to use subString or something similar to it because I need to use the original string for appending more characters '45'. With substring it will output 01245 but what I need is 01212345.

like image 739
Phon Soyang Avatar asked Oct 06 '16 02:10

Phon Soyang


People also ask

How do you get the first few characters of a string?

Use the String. slice() method to get the first N characters of a string, e.g. str. slice(0, 3) . The slice() method takes the start and stop indexes as parameters and returns a new string containing a slice of the original string.

How do you split the first 5 characters of a string?

You can use the substr function like this: echo substr($myStr, 0, 5);

How do you take the first 10 characters of a string?

How to find the first 10 characters of a string in C#? To get the first 10 characters, use the substring() method. string res = str. Substring(0, 10);


1 Answers

var str = '012123';  var strFirstThree = str.substring(0,3);    console.log(str); //shows '012123'  console.log(strFirstThree); // shows '012'

Now you have access to both.

like image 84
Alexander Nied Avatar answered Sep 30 '22 01:09

Alexander Nied