I have included special characters like @, |,~ but they also appeared as data in some values which breaks/fails my idea of joining and splitting values later in the code.
-
It depends on the types of values (e.g. phone numbers, policy ids, etc). What you can do is use multiple chars as separators (e.g. %%%) – symlink Dec 10 '22 at 05:37
-
1JSON.stringify() your data maybe instead of joining it? You can parse it back with `JSON.parse()` then – Nick Parsons Dec 10 '22 at 05:42
-
Does this answer your question? [What's the best separator/delimiter character(s) for a plaintext db file?](https://stackoverflow.com/questions/6319551/whats-the-best-separator-delimiter-characters-for-a-plaintext-db-file) – kmoser Dec 10 '22 at 06:20
2 Answers
There isn't any "safest" and "most reliable" separator to join string in any languages, not just JavaScript.
It depends entirely on your dataset, meaning the "safest" choice will be different for every different set of data.
For example, if your dataset is guaranteed to contain only integers, then any non-numeric characters can be the safest choice.
However, if your dataset is a free text, then there will be no "safest" choice, because even if you choose an arbitrary combination of string as the separator, i.e. %%%
, an end-user can still supply that data in a legit sentence like My preferred pronoun is "%%%"
, albeit highly unlikely. Thus using %%%
as separator here would still break your logic.
Because of this, you can only choose a separator that gives you the least risk.
Depending on your use case, there probably are other simpler solutions that does not require separators.

- 6,898
- 3
- 19
- 43
Generally we avoid joining strings if you need to separate them again later, JSON notation from serializing the data is usually a good compromise and has best interoperability.
CSV can work well too, but don't just insert commas, make sure you properly escape the values if they need it.
If JSON or CSV isn't appropriate, then using a sequence of special characters is more likely to be unique, you could use ||
(double pipe) as that is very unlikely to occur in anything except C based code.
You could use other special characters but I would avoid $
or %
as these are commonly used in replacement tokens. Also avoid any form of brackets as they are used for other container based replacement.
A 3 character code using multiple symbols is more unique again |:|
just pick something that visually looks like a barrier between values and can't be confused with tokens.

- 13,704
- 3
- 43
- 81