Insert Delimiter into a Text After N or Every N Character in Google Sheets

Is there a way to insert a delimiter into a text string after a certain number of characters in Google Sheets? Absolutely! In fact, you can do it using a powerful function called REGEXREPLACE. This function allows you to add a delimiter after every N character or after a specific N character in your text string.

Adding a delimiter to a text string can be incredibly useful in various scenarios, such as splitting a text at every N character, inserting a separator like a comma or pipe after every N character, or even splitting a text after a specific N character. Let’s explore how you can achieve this in Google Sheets using REGEXREPLACE.

Adding Delimiter After Every N Character

To add a delimiter after every N character in a text string, you can use the following generic formula:

=REGEXREPLACE(cell_ref, ".{N}", "$0,")

For example, if you want to insert a comma after each character in cell A1, you can use the formula:

=REGEXREPLACE(A1, ".{1}", "$0,")

The number 1 in the formula represents the value of N, which is the number of characters after which you want to insert the delimiter. If you change the value of N to 2, the formula will insert a comma after every 2 characters.

Splitting at Every N Character

To split a text string at every N character, you can combine the above formula with the SPLIT function. For example:

=SPLIT(REGEXREPLACE(A1, ".{1}", "$0|"), "|")

In this formula, I’ve used the pipe symbol “|” as the delimiter. You can change it to any other character that suits your needs.

Adding Delimiter After N Character

If you want to insert a delimiter after a specific N character in your text string, you can modify the formula slightly. Here’s the generic formula for this scenario:

=REGEXREPLACE(cell_ref, "^.{N}", "$0,")

For instance, if you want to insert a hyphen after the 13th character in cell A1, you can use the following formula:

=REGEXREPLACE(A1, "^.{13}", "$0-")

You can adjust the value of N as per your requirement.

Splitting After N Character

Similar to the previous case, you can also split a text string after a specific N character using the SPLIT function. Just combine it with the modified formula. For example:

=SPLIT(REGEXREPLACE(A1, "^.{1}", "$0|"), "|")

The above formula will split the text string after the 1st character.

Delimiters in Numbers

But what if you have numbers instead of a text string? Can you still add delimiters using REGEXREPLACE? Yes, you can! However, the default formula may return a #VALUE! error as it expects text values. To work around this, you have two options:

  1. Format the number as plain text by selecting Format > Number > Plain text from the menu.
  2. Use the TO_TEXT function within REGEXREPLACE to convert numbers to text. For example:

=REGEXREPLACE(TO_TEXT(A1), "^.{3}", "$0-")

Array Formulas for Bulk Processing

Since REGEXREPLACE is not an array formula, you can use the ARRAYFORMULA function to add delimiters to multiple cells at once. For numbers, use the following formula:

=ArrayFormula(if(A1:A="",,(REGEXREPLACE(TO_TEXT(A1:A),"^.{3}", "$0-"))))

And for text strings:

=ArrayFormula(if(A1:A="",,(REGEXREPLACE(A1:A,"^.{3}", "$0-"))))

These formulas will save you time and effort when dealing with large datasets.

And there you have it! You now know how to insert a delimiter into a text string after N or every N character in Google Sheets. With the power of REGEXREPLACE and a little creativity, you can accomplish various tasks, from splitting text strings to formatting numbers. So go ahead and try it out in your own Google Sheets!

For more exciting tips and tricks on Google Sheets and other handy tools, visit Crawlan.com. Happy delimiting!

Related posts