substr                 package:base                 R Documentation

_S_u_b_s_t_r_i_n_g_s _o_f _a _C_h_a_r_a_c_t_e_r _V_e_c_t_o_r

_D_e_s_c_r_i_p_t_i_o_n:

     Extract or replace substrings in a character vector.

_U_s_a_g_e:

     substr(x, start, stop)
     substring(text, first, last = 1000000)
     substr(x, start, stop) <- value
     substring(text, first, last = 1000000) <- value

_A_r_g_u_m_e_n_t_s:

 x, text: a character vector

start, first: integer. The first element to be replaced.

stop, last: integer. The last element to be replaced.

   value: a character vector, recycled if necessary.

_D_e_t_a_i_l_s:

     'substring' is compatible with S, with 'first' and 'last' instead
     of 'start' and 'stop'. For vector arguments, it expands the
     arguments cyclically to the length of the longest _provided_ none
     are of zero length.

     When extracting, if 'start' is larger than the string length then
     '""' is returned.

     For the replacement functions, if 'start' is larger than the
     string length then no replacement is done.  If the portion to be
     replaced is longer than the replacement string, then only the
     portion the length of the string is replaced.

_V_a_l_u_e:

     For 'substr', a character vector of the same length as 'x'.

     For 'substring', a character vector of length the longest of the
     arguments.

_N_o_t_e:

     The S4 version of 'substring<-' ignores 'last'; this version does
     not.

     These functions are often used with 'nchar' to truncate a display.
      That does not really work (you want to limit the width, not the
     number of characters, so it would be better to use 'strtrim'), but
     at least make sure you use 'nchar(type="c")'.

_R_e_f_e_r_e_n_c_e_s:

     Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988) _The New S
     Language_. Wadsworth & Brooks/Cole. ('substring'.)

_S_e_e _A_l_s_o:

     'strsplit', 'paste', 'nchar'.

_E_x_a_m_p_l_e_s:

     substr("abcdef",2,4)
     substring("abcdef",1:6,1:6)
     ## strsplit is more efficient ...

     substr(rep("abcdef",4),1:4,4:5)
     x <- c("asfef", "qwerty", "yuiop[", "b", "stuff.blah.yech")
     substr(x, 2, 5)
     substring(x, 2, 4:6)

     substring(x, 2) <- c("..", "+++")
     x

