Search code examples
rstringstring-length

How to find the length of the first line of a string in a multi line string in R


I am working on the following problem in R

[ Note: I realize that there is an answer posted to a similar question which is

How do I get the length of the first line in a multi line string?

however I am looking for a solution in R, not in python. ]

Suppose I have a string on multiple lines such as the one below:

"The first line of the string,
the second line of the string
and finally the the third line of the string."

I would like to know the length of just the first line of the string which is:

> nchar("The first line of the string,")
[1] 29

However the string is really stored as

> "The first line of the string,
+ the second line of the string
+ and finally the the third line of the string"
[1] "The first line of the string,\nthe second line of the string\nand finally the the third line of the string"

So when I apply the nchar function to the entire string, I get

> my_string = "The first line of the string,
+ the second line of the string
+ and finally the the third line of the string"
> nchar(my_string)
[1] 104

which is not correct.

Is there a way I can get the number of characters on just the first line?


Solution

  • You can use sub to remove everything after \n and use then nchar.

    nchar(sub("\n.*", "", my_string))
    #[1] 29
    

    or using strsplit

    nchar(strsplit(my_string, "\n")[[1]][1])
    #nchar(strsplit(my_string, "\n")[[c(1,1)]]) #Alternative
    #[1] 29