R/strcap.R

Defines functions strcap

Documented in strcap

##' To capitalize every first letter of a word.
##' 
##' @title Mixed case capitalizing.
##' @param string A character vector.
##' @param strict Whether strict. 
##' @return A character vector with the first letter of each word capitalized.
##' @author Jian Li <\email{[email protected]@sina.com}>
##' @keywords string
##' @examples
##' strcap("the quick red fox jumps over the lazy brown dog")
##'

strcap <- function(string, strict = FALSE) {
	string <- .verifyChar(string)
	cap <- function(s) paste(toupper(substring(s, 1, 1)),
				{s <- substring(s, 2); if(strict) tolower(s) else s},
				sep = "", collapse = " ")
	idxspace <- which(!nzchar(strstrip(string)))
	OUT <- sapply(strsplit(string, split = " "), cap, USE.NAMES = !is.null(names(string)))
	if (length(idxspace) > 0) OUT[idxspace] <- string[idxspace]
	return(OUT)
}

Try the tmcn package in your browser

Any scripts or data that you put into this service are public.

tmcn documentation built on March 18, 2018, 1:44 p.m.