Try this:

gsub("^\\s+|\\s+$", "", "       this is random text        ")

On Wed, Jul 7, 2010 at 10:23 AM, Andrew Leeser <aml05willi...@yahoo.com>wrote:

> I am looking for a way to trim leading and trailing spaces in a character
> string in R. For example:
>
> "       this is random text        "
>
> should become:
>
> "this is random text".
>
> I have a short function to perform this task as follows:
>
> trim <- function(str){
>        str <- sub("^ +", "", str)
>        str <- sub(" +$", "", str)
> }
>
> While this function does seem to work, I am curious if there's anything
> built
> into R that I can use instead, as that would be preferable.
>
> Thanks in advance,
> Andrew
>
>
>
>        [[alternative HTML version deleted]]
>
>
> ______________________________________________
> R-help@r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
>


-- 
Henrique Dallazuanna
Curitiba-Paraná-Brasil
25° 25' 40" S 49° 16' 22" O

        [[alternative HTML version deleted]]

______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to