R/v1-textUtils.R

Defines functions firstChar lastChar char quotify

# Function to add double quotes onto the start and end of the strings
quotify <- function(x) {
    paste("\"", x, "\"", sep="")
}

# Function to get the nth character of a string
char <- function(string, n) {
    checkThat(string, isString)

    if (any(abs(n) > nchar(string))) {
        stop(paste(quotify(n), "is outside index range"))
    }

    if (n == 0) {
        return("")
    }

    if (n < 0) {
        n <- nchar(string) + n + 1
    }

    return(substr(string, n, n))
}

lastChar <- function(string) {
    char(string, nchar(string))
}

firstChar <- function(string) {
    char(string, 1)
}

Try the Glimma package in your browser

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

Glimma documentation built on Nov. 8, 2020, 6:13 p.m.