Trish Pham
Trish Pham

Reputation: 479

Strip words after @

How do I strip out the words after the @? for example if string is

12 @ 8.97 MB

then the output would become

12

How to do this in javascript?

Upvotes: 0

Views: 73

Answers (3)

VicoMan
VicoMan

Reputation: 289

try:

var mystring = "12@something";
return mystring.substring('@')[0];

Upvotes: 0

Fahad
Fahad

Reputation: 1434

Try this:

var index = str.indexOf('@', 0);
if (index != -1)
    var output = str.substring(0, index);
else 
    var output = str;

Hope it helps :)

Upvotes: 2

Elias Van Ootegem
Elias Van Ootegem

Reputation: 76395

just split it, if there is no @ char, it'll just return an array of 1 string, if there was an @, everything that preceded the @, will be assigned to the first (0) index of the resulting array. In other words:

 var preAt = someString.split('@')[0];

It doesn't matter if the string contained 0 or 1000 at signs, this will work

Upvotes: 1

Related Questions