[R] Function for trim blanks from a string(s)?

S Ellison S.Ellison at lgc.co.uk
Tue Aug 7 15:04:22 CEST 2007

Stripping either or both ends can be achieved by the somewhat tortuous but fairly general

stripspace<-function(x) sub("^\\s*([^ ]*.*[^ ])\\s*$", "\\1",x)

which uses a replacement buffer to keep the useful part of the string.

>>> Prof Brian Ripley <ripley at stats.ox.ac.uk> 06/08/2007 21:23:49 >>>
I am sure Marc knows that ?sub has examples of trimming trailing space and 
whitespace in various styles.

This email contains information which may be confidential and/or privileged, and is intended only for the individual(s) or organisation(s) named above. If you are not the intended recipient, then please note that any disclosure, copying, distribution or use of the contents of this email is prohibited. Internet communications are not 100% secure and therefore we ask that you acknowledge this. If you have received this email in error, please notify the sender or contact +44(0)20 8943 7000 or postmaster at lgcforensics.com immediately, and delete this email and any attachments and copies from your system. Thank you. 

LGC Limited. Registered in England 2991879. 
Registered office: Queens Road, Teddington, Middlesex TW11 0LY, UK

More information about the R-help mailing list