Home javascript How to trim js string?

How to trim js string?

Author

Date

Category

There is a line: Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Its length may vary. How can I trim a string so that only the first 10 characters remain?


Answer 1, authority 100%

You can use substring

"Lorem ipsum ...". substring (0, 10);

Or substr

"Lorem ipsum ...". substr (0, 10);

Or slice

"Lorem ipsum ...". slice (0, 10);

Answer 2, authority 10%

slice method can be used
More details here https://developer.mozilla.org/ ru / docs / Web / JavaScript / Reference / Global_Objects / String / slice

var string = "Lorem ipsum dolor sit amet, consectetur adipiscing elit.";
console.log (string.slice (0, 10)) 

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