Become an expert in R — Interactive courses, Cheat Sheets, certificates and more!
Get Started for Free

trimSpace

Trim leading and/or trailing spaces in strings


Description

This function removes from a character vector the longest successive run of space characters starting at the begining of the strings (leading space), or the longest successive run of space characters at the end of the strings (trailing space), or both (and this is the default behaviour).

Usage

trimSpace(x, leading = TRUE, trailing = TRUE, space = "[:space:]")

Arguments

x

a character vector

leading

logical defaulting to TRUE: should leading spaces be trimed off?

trailing

logical defaulting to TRUE: should trailing spaces be trimed off?

space

an extended regular expression defining space characters

Details

The default value for the space character definition is large: in addition to the usual space, other character such as the tabulation and newline character are considered as space characters. See extended regular expression for a complete list.

Value

a character vector with the same length as x.

Author(s)

J.R. Lobry

References

citation("seqinr").

See Also

Extended regular expressionsare described in regular expression (aka regexp).

Examples

#
  # Simple use:
  #
stopifnot( trimSpace("   seqinR   ") == "seqinR" )

  #
  # Basic use, remove space at both ends:
  #
testspace <- c("   with leading space", "with trailing space   ", "   with both   ")
stopifnot(all( trimSpace(testspace) == c("with leading space", 
                                         "with trailing space", 
					 "with both")))

  #
  # Remove only leading space:
  #
stopifnot(all( trimSpace(testspace, trailing = FALSE) == c("with leading space",
                                                           "with trailing space   ",  
							   "with both   ")))

  #
  # Remove only trailing space:
  #
stopifnot(all( trimSpace(testspace, leading = FALSE) == c("   with leading space", 
                                                          "with trailing space",  
							  "   with both")))

  #
  # This should do nothing:
  #
stopifnot(all( trimSpace(testspace, leading = FALSE, trailing = FALSE) == testspace))

  #
  # How to use alternative space characters:
  #
allspaces <- "\t\n\f\r seqinR \t\n\f\r"
stopifnot(trimSpace(allspaces) == "seqinR")
stopifnot(trimSpace(allspaces, space = "\t\n") == "\f\r seqinR \t\n\f\r")

seqinr

Biological Sequences Retrieval and Analysis

v4.2-16
GPL (>= 2)
Authors
Delphine Charif [aut], Olivier Clerc [ctb], Carolin Frank [ctb], Jean R. Lobry [aut, cph], Anamaria Necşulea [ctb], Leonor Palmeira [ctb], Simon Penel [cre], Guy Perrière [ctb]
Initial release
2022-05-19

We don't support your browser anymore

Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.