Better use of split() and join()

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,


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.

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