Quantcast
Channel: Get everything after the dash in a string in JavaScript - Stack Overflow
Viewing all articles
Browse latest Browse all 15

Answer by axwr for Get everything after the dash in a string in JavaScript

$
0
0

Everyone else has posted some perfectly reasonable answers. I took a different direction. Without using split, substring, or indexOf. Works great on i.e. and firefox. Probably works on Netscape too.

Just a loop and two ifs.

function getAfterDash(str) {    var dashed = false;    var result = "";    for (var i = 0, len = str.length; i < len; i++) {        if (dashed) {            result = result + str[i];        }        if (str[i] === '-') {            dashed = true;        }    }    return result;};console.log(getAfterDash("adfjkl-o812347"));

My solution is performant and handles edge cases.


The point of the above code was to procrastinate work, please don't actually use it.


Viewing all articles
Browse latest Browse all 15

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>