Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Remove second part of string with split()

I have a variable with a string, let’s say this one, which I then display on a page on the site:

let value = "qwe asd — bensound summer";

document.getElementById("text").innerHTML = value;

And I want to remove its second part when displaying this line on the page bensound summer along with a dash .

And in order to receive only the first part, which is before the dash, when displayed on the page, in the form: qwe asd.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I read about str.split() but didn’t find anything like it and didn’t quite understand how it all works.

>Solution :

Use the split method

let value = "qwe asd - bensound summer";

value = value.split('-')[0] // 

document.getElementById("text").innerHTML = value;
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading