Javascript - Substrings starting at the end and moving

时间:2019-04-17 02:10:49

标签: javascript

I am not sure if this is a duplicate but I need to break a string into substrings to place time dividers into it. I want to turn 2016 into 20.16 and 21070 into 2:10.70 by starting that the length of the string and moving 2 places in reverse.

I tried the following on 2016:

best.toString().slice(-2, best.toString().length); //16
best.toString().slice(-2, (best.toString().length - 2)); //blank

I tired the following on 21070:

best.toString().slice(-2, best.toString().length); //70
best.toString().slice(-2, (best.toString().length - 2)); //blank
best.toString().slice(-2, (best.toString().length - 4)); //blank

What am I missing with the starting point of the slice? Thanks!

5 个答案:

答案 0 :(得分:1)

You don't even need to use slice

function format(best) {
   const a = (best / 10000) | 0;
   const b = ((best / 100) | 0) % 100;
   const c = (best % 100)

   let result = '';
   if (a > 0) result += a.toString() + ':';
   result += b.toString().padStart(2, '0') + '.';
   result += c.toString().padStart(2, '0');

   return result;
}

format(2016);   // 20.16
format(21070);  // 2:10.70

答案 1 :(得分:0)

You have to think of how the length is referenced. For '2016', the length is 4.

[0] - 2
[1] - 0
[2] - 1
[3] - 6

You would want to start with the last position - 2 which would be 3 - 2 = 1 and then move 2 places.

To get 16, you would want

const str = best.toString();
const lastTwoChars = str.slice(best.length - 2, 2);

答案 2 :(得分:0)

Look at this alternative using Regex and captured groups.

console.log("2016".replace(/(\d{2})(\d{2})/, "$1.$2"));
console.log("21070".replace(/(\d{1})(\d{2})(\d{2})/, "$1:$2.$3"));

答案 3 :(得分:0)

slice() accept start position and end position. Negative number means counting from the end. The start position must be on the left hand side of the end position otherwise you would get blank. If you gonna use slice, you should change the start position too:

console.log("21070".slice(-2));
console.log("21070".slice(-4,-2));
console.log("21070".slice(-6,-4));

答案 4 :(得分:0)

For your main question about how to separate it, I can see many answers

But I'm interested in negative arguments in starting point and end point

Based on http://www.tothenew.com/blog/javascript-slice-vs-substring-vs-substr/

slice() method can take 2 arguments:

Argument 1: begin, Required. The position where to begin the extraction. First character is at position 0. Use negative values to specify the position from the end of the string.

Argument 2: end, Optional. The position (up to, but not including) where to end the extraction. If omitted, slice() selects all characters from the start-position to the end of the string.Use negative numbers to select from the end of the string.

So

best = 2016

best.toString().slice(-2, best.toString().length); //16

Starting point = -2 mean It starts from 1

End point = length means It ends at end of string

So you get 16 as result

best.toString().slice(-2, (best.toString().length - 2)); //blank

Starting point is -2 mean It starts from 1

End point = length - 2 = 2 means It ends at starting point

So you get blank as result