![]() |
VOOZH | about |
In this article, we are going to see how to get the number of characters in a string in R Programming Language. The number of characters in a string refers to the length of the string.
Examples:
Input: Geeksforgeeks
Output: 13
Explanation: Total 13 characters in the given string.Input: Hello world
Output: 11
This can be calculated using thebelow-mentioned functions:
nchar() method in R programming is used to get the length of a string.
Syntax:
nchar(string)
Parameter: string
Return: Returns the length of a string
Example: To calculate length
[1] 15
The string "Geeks For Geeks" has 15 characters, including spaces.
This method can be used to return the length of multiple strings passed as a parameter.
[1] 12 6
The function str_length() belonging to the ‘stringr’ package can be used to determine the length of strings in R.
Syntax:
str_length (str)
Parameter: string as str
Return value: Length of string
Example: To find length
Output:
[1] 5This method can be used to return the length of multiple strings passed as a parameter.
Output:
[1] 12 6This function returns the number of code points in each string.
Syntax:
stri_length(str)
Parameter: str as character vector
Return Value: Returns an integer vector of the same length as str.
Example:
Output:
[1] 6This method can be used to return the length of multiple strings passed as a parameter.
Output:
[1] 11 6