In R, you can use the substr()
function with nchar()
to get the last character of a string.
In this example,
str
with a value of "Hello World"
.nchar()
function to get the length of str
and then use substr()
with str
and nchar(str)
to get the last character.str <- "Hello World"
last_char <- substr(str, nchar(str), nchar(str))
cat("Last character of the string is: ", last_char)
Last character of the string is: d
In this example,
str
with a value of "apple"
.nchar()
function to get the length of str
and then use substr()
with str
and nchar(str)
to get the last character.str <- "apple"
last_char <- substr(str, nchar(str), nchar(str))
cat("Last character of the string is: ", last_char)
Last character of the string is: e
In this tutorial, we learned How to get Last Character in String in R language with well detailed examples.