Draw characters in variable

Asked

Viewed 1,128 times

0

I have a variable with the following content 00:00:01. I want to take out the two points to have the final result 000001. I use the split but the result I have is 00,00,01.

var res = min_time.split(':');

3 answers

5


Use the function replace(), replacing string. The first parameter is string which must be replaced and the second parameter is what will replace the first. The first parameter accepts both a regular expression as a string. Example:

var teste = "00:00:01";
var teste2 = teste.replace(/:/g, "");
alert(teste2);

Remembering that when using string it will replace only the first occurrence. Example:

var teste = "00:00:01";
var teste2 = teste.replace(":", "");
alert(teste2);

2

So when Voce uses the command split, it breaks the "word" and transforms it into an array, each snippet it finds separated by the delimiter : it will store in an array space, if you want to continue using it in this way, just Voce do the following:

hora = res[2]+''+res[1]+''+res[0];

Important detail: Javascript is very annoying when typing objects, if you simply do something like:

res[2]+res[1]+res[0];

Instead of considering it as a string and concatenate to have the expected result, it will add the parts, because when reading a number, it will interpret as an object of type number, so there’s a need for single quotes...

Note: I recommend using the command replace, according as @Earendul passed ;]

2

By default, the . toString() of a matrix will use the comma as delimiter, you can also do as follows:

var res = min_time.split(':').join('');

take a look at this link. Join

Browser other questions tagged

You are not signed in. Login or sign up in order to post.