str_sub and str_replace

Hi, just trying to figure out str_sub and str_replace. The instructions seemed pretty straightforward but when I tried it, it gave me strange behavior.

Column = c("2020-11-02-", "2020-11-02-")
str_replace(Column, ".",":") //not sure why it replaced 2 with .

Column2 = str_sub(Column,1,14) <- ":"
Column2 //not sure why nothing showed up other than the :

using str_replace, it replaced 2020 with :0202
using str_sub, it replaced the vector with :


str_* functions use "regular expressions" (regex) and in regex, . is a metacharacter that matches "any character", if you want to replace a literall ".", you have to scape the metacharacter with \\. try

str_replace_all(Column, "\\.",":")
1 Like

worked like a charm.

Thank you my friend!

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.