Search code examples
rstringstringi

Extract information from string with dots as separator in R


I apologize for possible similar questions, but I just can't find the solution for my problem. So, I have a string with three parts, separated by “.”, for example:

a <- "XXX.YY.ZZZ"

(the length of strings differ, it could also be a <- "XXXX.Y.ZZ", but the three parts are always separated by the two “.”. I solved the problem for the first part:

library(stringi)
stri_extract(a, regex='[^.]*')
[1] "XXX"

Appreciate your help.


Solution

  • hello you can use strsplit as follows

       strsplit(a,"\\.")[[1]]