Become an expert in R — Interactive courses, Cheat Sheets, certificates and more!
Get Started for Free

cpos

Find the position of a substring


Description

cpos, cposV finds the first position of a substring;
cposR returns a list with starting and ending positions, works only with a single string;
issubstr checks if is a substring

Usage

cpos(str,sub,start=1)
cposV(vstr,sub,vstart=1)
cposR(str, sub, restrict)
issubstr(str,sub,start=1)

Arguments

str

string to examine

vstr

vector of strings to examine

sub

(vector of) substring to find

start,vstart

(vector of) integer, position(s) of start of search

restrict

vector of lower and upper index the search should be restricted to. If missing, whole 'str' is taken.

Value

cpos,cposL,cposV number, if found, NA otherwise.
cposR list(first,last) for each occurrence of sub within the restriction restrict of str; If there is none, then first=NA,last=NA.

Note

parameters in cposV will be recycled, so that all have the same (maximum) length.

Author(s)

Christian W. Hoffmann <christian@echoffmann.ch>

Examples

cpos(" Baldrian","a",5) #  3
  cpos("Baldrian","B",15) # NA
  cposR(" Baldabcrian  abcf","abc")
#$first  6 15
#$last   8 17
  cposR(" Baldabcrian  abcf","abc",c(2:16))
#$first 6
#$last  8
  cposV(c("Xcdbeesh","withh "),c("X","h","ees"),c(1,5))
# 1 4 5
issubstr("Today is a wonderful day","wonder")

cwhmisc

Miscellaneous Functions for Math, Plotting, Printing, Statistics, Strings, and Tools

v6.6
GPL (>= 2)
Authors
Christian W. Hoffmann
Initial release
2018-08-24, 10:40:10

We don't support your browser anymore

Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.