-3

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.

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

Krol
  • 13
  • 7
  • 1
    Does this answer your question? [Get Substring between two characters using javascript](https://stackoverflow.com/questions/14867835/get-substring-between-two-characters-using-javascript) – Tobias S. Apr 23 '22 at 16:13

2 Answers2

-1

Use the split method

let value = "qwe asd - bensound summer";

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

document.getElementById("text").innerHTML = value;
Gedeon Mutshipayi
  • 2,871
  • 3
  • 21
  • 42
-1

You can use it like this:

let value = "qwe asd — bensound summer";
let splittedString = value.split("-");

document.getElementById("text").innerHTML = splittedString[0];

Hope, it helps!!

ps2708
  • 132
  • 9