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}}
try this:> x <- ' middle of the string ' > sub("^[[:space:]]*(.*?)[[:space:]]*$", "\\1", x, perl=TRUE)[1] "middle of the string" 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. >-- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem that you are trying to solve?
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. >
This just saved me a lot of time. Thank you! Daniel -- View this message in context: http://r.789695.n4.nabble.com/Remove-leading-and-trailing-white-spaces-tp907851p4489725.html Sent from the R help mailing list archive at Nabble.com.