Better use of split() and join()

Better use of split() and join()
0

#1

I have a function that should clean (from “.”, “,” and " ") a given string, but I’m unsure if there isn’t smarter way as the code seems sloppy:

function CleanString(x) { return x.toString().split(".").join('').split(',').join('').split(' ').join(''); }

I know that regex is a solution as well, but I’m trying to avoid it. Is there a better way to use split() and join() in that case?

Best regards and thanks in advance,

Max


#2

In that case better way to split and join is to use regex (or chained replaces).

You must select best tool for the job and here the best tool would be regex.


#3

Thanks! I will try a solution based on regex :slight_smile: