Disclaimer: I KNOW that in 99% of cases you shouldn’t “serialize” data in a concatenated string.
What char you guys use in well-known situation:
string str = userId +"-"+ userName;
In majority of cases I have fallen back to | (pipe) but, in some cases users type even that. What about “non-typable” characters like ☼ (ALT+9999)?
That depends on too many factors to give a concrete answer.
Firstly, why are you doing this? If you feel the need to store the
userIdanduserNameby combining them in this fashion, consider alternative approaches, e.g. CSV-style quoting or similar.Secondly, under normal circumstances only delimiters that aren’t part of the strings should be used. If userId is just a number then “-” is fine… but what if the number could be negative?
Third, it depends on what you plan to do with the string. If it is simply for logging or debugger or some other form of human consumption then you can relax a bit about it, and just choose a delimiter that looks appropriate. If you plan to store data like this, use a delimiter than ensures you can extract the data properly later on, regardless of the values of
userIdoruserName. If you can get away with it, use\0for example. If either value comes from an untrusted source (i.e. the Internet), then make sure the delimiter can’t be used as a character in either string. Generally you would limit the characters that each contains – say, digits foruserIdand letters, digits and SOME punctuation characters foruserName.