The individual characters of a string can be extracted from a string by using the indexing methods of a string. There are two R’s inbuilt functions in order to access both the single character as well as the substrings of the string.
substr() or substring() function in R extracts substrings out of a string beginning with the start index and ending with the end index. It also replaces the specified substring with a new set of characters.
Syntax:
substr(..., start, end)
or
substring(..., start, end)
Example 1: Using substr() function
R program to access
characters in a string
# Accessing characters
using substr() function
substr("Learn Code Tech", 1, 1)
Output:
"L"
If the starting index is equal to the ending index, the corresponding character of the string is accessed. In this case, the first character, ‘L’ is printed.
Top comments (0)