R/hasChar.R

Defines functions hasSuffix hasPrefix

Documented in hasPrefix hasSuffix

# This function returns TRUE if a given prefix, suffix, or characters
# matches a character string or FALSE otherwise.
#
# Copyright 2002, J. Zhang. All rights reserved.
#

hasPrefix <- function(aPrefix){
    hasChar(aPrefix, "prefix")
}

hasSuffix <- function(aSuffix){
    hasChar(aSuffix, "suffix")
}

hasChar <- function (toCheck, what = ""){

    if(!is.character(toCheck) || nchar(toCheck)  < 1)
        stop(paste("Bad value:", toCheck))

    function(x){
        if(what == "prefix"){
            pattern <- paste("^", toCheck, sep = "")
        }else if(what == "suffix"){
            pattern <- paste(toCheck, "$", sep = "")
        }else{
            pattern <- toCheck
        }

        if(regexpr(pattern, x) > 0 ){
           return(TRUE)
        }else{
            return(FALSE)
        }
    }
}

Try the tkWidgets package in your browser

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

tkWidgets documentation built on Nov. 8, 2020, 5:17 p.m.