getCommonPrefix: Compute the longest common prefix of the given strings

View source: R/prefix.S

getCommonPrefixR Documentation

Compute the longest common prefix of the given strings

Description

This function finds the longest common prefix shared by the input strings. This is different from finding the longest common substring as here we are insisting that this occur at the beginning of each of the strings. This uses a straightforward search, terminating whenever it finds a mismatch in any of the strings.

Usage

getCommonPrefix(words, asCharacter = TRUE, na.rm = TRUE)

Arguments

words

a character vector containing the strings of interest

asCharacter

a logical value which if TRUE means that the actual prefix is returned. Otherwise, the number of characters in the prefix is returned and this can be used to compute the substring or for some other purpose.

na.rm

a logical value indicating whether to remove NAs. If this is FALSE and there are NA elements in words, an error is raised.

Value

If asCharacter is TRUE, a character vector giving the actual prefix. If there is no common prefix, this is the empty character vector.

If words is empty, this returns the empty character vector if asCharacter is TRUE and 0 otherwise.

If asCharacter is FALSE, this returns a scalar integer giving the position of the end character of the common prefix string.

Author(s)

Duncan Temple Lang <duncan@wald.ucdavis.edu>

References

http://www.omegahat.org/Rlibstree

See Also

getLongestCommonSubstring

Examples


   # The names of enumerated constants in Microsoft Word's
   # scripting interface.  We want to find the common prefix.
enumNames = c('wdSummaryModeHighlight',
              'wdSummaryModeHideAllButSummary',
              'wdSummaryModeInsert',
              'wdSummaryModeCreateNew')

 getCommonPrefix(enumNames)

omegahat/Rlibstree documentation built on Jan. 17, 2024, 6:37 p.m.