A string
like in every other programming language is used to store text. A string
is always written in single quotation marks (''
) or double quotation marks(""
). Examples are: 'Hello world'
, "Hello World",
etc.
These texts can also be referred to as characters. For instance, the first character in the string "Hello world"
is "H"
, the sixth character is a space " "
and the last character of the string is d
.
To check for the number of characters present in a string, we use the
nchar()
function of R.
nchar(string)
string
: Where the parameter value is the string
variable in which you wish to find the number of characters present in it.let’s create a string
and calculate the number of characters present in that string
.
# creating a stringmystring <- "Hello World!"# using the nchar() functionnchar(mystring)
From the output of the code above, we can see that using the nchar()
function, the string "Hello World"
contains just 12
characters.