[R] Remove leading and trailing white spaces

Gabor Grothendieck ggrothendieck at gmail.com
Fri Nov 20 17:03:53 CET 2009


See:
http://finzi.psych.upenn.edu/R/Rhelp02a/archive/40714.html

There are many packages which have this functionality as well and a
search (??, RSiteSearch, rseek.org) will find them.

The links box on the http://gsubfn.googlecode.com page has links to
regular expression pages on the web.

On Fri, Nov 20, 2009 at 10:51 AM, Bos, Roger <roger.bos at rothschild.com> wrote:
> I have a character string and I would like to remove the leading and
> tailing white spaces.  The example for 'sub' shows how to remove the
> trailing white spaces, but I still can't figure out how to remove both
> trailing and leading white spaces because I can't find any documentation
> for what "+$" means or what "\\s+$" means.  Maybe its because I don't
> have a Unix background.  Thanks in advance for any help with this.
>
> str <- '    Now is the time      '
> sub(' +$', '', str)  ## spaces only
> sub('[[:space:]]+$', '', str) ## white space, POSIX-style
> sub('\\s+$', '', str, perl = TRUE) ## Perl-style white space
>
> Thanks,
>
> Roger
> ***************************************************************
>
> This message is for the named person's use only. It may\...{{dropped:23}}
>
> ______________________________________________
> R-help at 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.
>




More information about the R-help mailing list