nchar                  package:base                  R Documentation

_C_o_u_n_t _t_h_e _N_u_m_b_e_r _o_f _C_h_a_r_a_c_t_e_r_s (_B_y_t_e_s)

_D_e_s_c_r_i_p_t_i_o_n:

     'nchar' takes a character vector as an argument and returns a
     vector whose elements contain the sizes of the corresponding
     elements of 'x'.

_U_s_a_g_e:

     nchar(x, type = c("bytes", "chars", "width"))

_A_r_g_u_m_e_n_t_s:

       x: character vector, or a vector to be coerced to a character
          vector.

    type: character string: partial matching is allowed.  See Details.

_D_e_t_a_i_l_s:

     The 'size' of a character string can be measured in one of three
     ways

     '_b_y_t_e_s' The number of bytes needed to store the string (plus in C
          a final terminator which is not counted).

     '_c_h_a_r_s' The number of human-readable characters.

     '_w_i_d_t_h' The number of columns 'cat' will use to print the string
          in a monospaced font.  The same as 'chars' if this cannot be
          calculated (which is currently common).

     These will often be the same, and always will be in single-byte
     locales. There will be differences between the first two with
     multibyte character sequences, e.g. in UTF-8 locales. If the byte
     stream contains embedded 'nul' bytes, 'type = "bytes"' looks at
     all the bytes whereas the other two types look only at the string
     as printed by 'cat', up to the first 'nul' byte.

     The internal equivalent of the default method of 'as.character' is
     performed on 'x'.  If you want to operate on non-vector objects
     passing them through 'deparse' first will be required.

_V_a_l_u_e:

     An integer vector giving the size of each string, currently always
     '2' for missing values (for 'NA').

     Not all platforms will return a non-missing value for
     'type="width"'.

     If the string is invalid in a multi-byte character set such as
     UTF-8, the number of characters and the width will be 'NA'. 
     Otherwise the number of characters will be non-negative, so
     '!is.na(nchar(x, "chars"))' is a test of validity.

_N_o_t_e:

     This does *not* by default give the number of characters that will
     be used to 'print()' the string, although it was documented to do
     so up to R 2.0.1.  Use 'encodeString' to find the characters used
     to print the string.

     As from R 2.1.0 embedded 'nul' bytes are included in the byte
     count (but not the final 'nul'): previously the count stopped
     immediately before the first 'nul'.

_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.

_S_e_e _A_l_s_o:

     'strwidth' giving width of strings for plotting; 'paste',
     'substr', 'strsplit'

_E_x_a_m_p_l_e_s:

     x <- c("asfef","qwerty","yuiop[","b","stuff.blah.yech")
     nchar(x)
     # 5  6  6  1 15

     nchar(deparse(mean))
     # 18 17

