To get all possible substrings of a string in R, you can use a combination of substrings and nested loops to generate all substrings.
In this example,
str
with a value of 'Hello'
.substrings
to store all substrings.substring
function and add it to the substrings
vector.substrings
vector.str <- 'Hello'
substrings <- character()
for (i in 1:nchar(str)) {
for (j in i:nchar(str)) {
substrings <- c(substrings, substr(str, i, j))
}
}
print(substrings)
[1] "H" "He" "Hel" "Hell" "Hello" "e" "el" "ell" "ello" "l" "ll" "llo" "lo" "o"
In this tutorial, we learned How to get all the possible Substrings of a String in R language with well detailed examples.