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). Thanks, Doug Grove
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
> 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). > > Thanks, > Doug Grovesub ("^[ \t]*", "", sub ("[ \t]*$", "", " hello "))
As an extension of the recent discussion of "functions different in R and S", what suggestions do people have for writing transportable code for R/S beyond Venables and Ripley (2000, S Programming, pp. 202-203)? For example, I recently found myself using the following construct: if(version$major < 5){ # Function oldClass was introduced with the "new S" # and is not present in R or S-Plus 2000 oldClass <- class } Similarly, I plan to define a function "gsub" if(is.null(version$language)). Do these seem reasonable? Are there other suggestions / standard / compilations of thoughts? Thanks, Spencer Graves