Click to See Complete Forum and Search --> : trim string


michelle
09-17-2003, 08:19 AM
I have a string that has a lot of \n (newlines) in the beginning. Is there some way I can trim them off?

I tried:
function stripNL(oldString) {

return oldString.replace(/\n/g, "");

}

But it does not seem to work.
Is there a good function or a built in function for trimming whitespaces and newlines of the beginning and end of a string?

// Michelle

Khalid Ali
09-17-2003, 08:25 AM
you ca write a pure JS function,however, that will not be elegant. look for any of these gentlement,they may be able to help you write a neat little regexp function

(Pyro,Adam,Charles)

Jeff Mott
09-17-2003, 08:42 AM
oldString.replace(/\n/g, "");This will remove new lines from anywhere within the string, but should still remove the new lines from the beginning of the string. What are you left with that you say it doesn't work?

In any case, you can remove any white space from the beginning and end of a string with_string_ = _string_.replace(/^\s+/, "").replace(/\s+$/, "");

michelle
09-17-2003, 09:57 AM
Thanks jeff.
That did the trick. I am not sure why my little function didn't work, but yours did and I am happy with that...

Regards,
// Michelle