[R] removing leading/trailing blanks
Sundar Dorai-Raj
sundar.dorai-raj at pdf.com
Wed Feb 19 22:14:04 CET 2003
Douglas Grove wrote:
> Hi,
>
> What's the best way of dropping leading or trailing
> blanks from a character string?
>
> The only thing I can think of is using sub() to replace
> blanks with null strings, but I don't know if there is
> a better way (I also don't know how to represent the
> trailing blank in a regular expression).
>
I would use gsub instead:
R> ch = " lkj df klj lkjsdf "
R> ch
[1] " lkj df klj lkjsdf "
R> gsub("^ .", "", ch) # remove leading white space
[1] "lkj df klj lkjsdf "
R> gsub(". $", "", ch) # remove trailing white space
[1] " lkj df klj lkjsdf"
R>
Regards,
Sundar
More information about the R-help
mailing list