'Remove portion of string in Javascript

I have a String like this in Javascript:

string = '@usernameWhats on your mind?'

I need to get rid of 'Whats on your mind?' from the string.

How would I do that?



Solution 1:[1]

var new_string = string.replace('Whats on your mind?', '');

Solution 2:[2]

At first, you can split the string:

string.split("@username"); // ["", "What on your mind?"]

Then pop removes the last element.

const newString = string.split("@username").pop();

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 jondavidjohn
Solution 2