Home javascript How to trim a line?

How to trim a line?

Author

Date

Category

There is a line var a = "13:45 PM" I want to trim PM and leave only 13:45
Tried it like this var b = str.substring (5,7) doesn’t work …


Answer 1, authority 100%

For example, like this:

console.log ("13:45 PM" .split ('') [0] ); // Split by space, take first part
console.log ("13:45 PM" .match (/ \ d? \ d: \ d \ d /) [0]); // match by regular expression
console.log ("13:45 PM" .substr (0, 5)); // 5 characters starting from 0
console.log ("13:45 PM" .substring (0, 5)); // copy by indices [0: 5)
console.log ("13:45 PM" .replace (/ \ s. * /, '')); // replace the space and everything else with an empty string
console.log ("13:45 PM" .slice (0, -3)); // slice without the last three characters 

Answer 2, authority 18%

var a = "13:45 PM";
var str = a.slice (0.5);
console.log (str); 

Answer 3, authority 9%

You are not using the correct String. prototype.substring . This method copies the string between the specified indices , rather than cutting it out (as you might think).

Here’s how to use this method correctly:

var a = '13: 45 PM ';
var b = a.substring (0, 6);
console.log (b);

Answer 4, authority 9%

. slice (0, -3)

document.querySelector ('button'). onclick = function () {
 document.querySelector ('output'). innerHTML = document.querySelector ('time'). innerHTML.slice (0, -3);
} 
& lt; time & gt; 10:15 AM & lt; / time & gt;
& lt; button & gt; slice me! & lt; / button & gt;
& lt; br & gt;
& lt; output & gt; & lt; / output & gt; 

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions