Home > Blockchain >  format string to specific format including dots and dashes
format string to specific format including dots and dashes

Time:08-16

I have a string with numbers only and it always has the same witdh. I need this string in a specific format.

original = "00000000000000"
outcome = "00.000.000/0000-00"

Is there a way simple way to do this? Could it be applied to a vector of strings?

CodePudding user response:

Assuming the width is constant, we can use sub:

original = "00000000000000"
sub("(.{2})(.{3})(.{3})(.{4})(.{2})", "\\1.\\2.\\3/\\4-\\5", original)
# [1] "00.000.000/0000-00"
  • Related