R/commonSubstring.R

#' commonSubstring
#'
#' Utility to find starting common substring in vector of strings
#' @param x Vector of strings
#' @return String containing blank or common substring.
#' @export
#' @examples
#' s <- commonSubstring(c('C','CACA'))
commonSubstring<-function(x) {
  # sort the vector
  x<-sort(x)
  # split the first and last element by character
  d_x<-strsplit(x[c(1,length(x))],"")
  # search for the first not common element and so, get the last matching one
  der_com<-match(FALSE,do.call("==",d_x))-1
  # if there is no matching element, return an empty vector, else return the common part
  ifelse(der_com==0,return(character(0)),return(substr(x[1],1,der_com)))
}
gdelangel/genomicRTools documentation built on May 16, 2019, 11:14 p.m.