commonPrefix: Longest common prefix

commonPrefixR Documentation

Longest common prefix

Description

Finds and returns the longest prefix common to all strings in a character vector. Can be set to ignore case, in which case the returned common string will be in lower case. If any strings are NA, this returns NA unless dropNA= TRUE is set, which will drop NAs before checking for a common prefix.

Usage

commonPrefix(x, ignoreCase = FALSE, dropNA = FALSE)

Arguments

x

The strings to find the longest common prefix for.

ignoreCase

Set this true to match prefixes even if they differ in case.

dropNA

Set this true to ignore NAs when searching for a common prefix.

Value

The common prefix, if any, or "", if no common prefix can be found.

See Also

commonSuffix

Examples

commonPrefix( c( "ABCDE", "ABC", "ABc" ))
#=> [1] "AB"

commonPrefix( c( "ABC", "abc", "def" ))
#=> [1] ""

commonPrefix( c( "ABCDE", "ABC", "" ))
#=> [1] ""

commonPrefix( c( "ABCDE", "ABC", NA ))
#=> [1] NA

commonPrefix( c( "A\u00dfCDE", "A\u00dfC", "A\u00dfc" ))
#=> [1] "Aß"

commonPrefix( c("ABCDE", "ABC", "ABc" ), ignoreCase= TRUE )
#=> [1] "abc"


jefferys/JefferysRUtils documentation built on Jan. 12, 2024, 9:18 p.m.