When we want to split a sting for any kind of reasons, we (at least myself) tend to split using the (pipe) |
character as it is very rare to find out someone or that the application uses it on a string ... but what happens if it uses?
Well, a simple Crash will be thrown :)
I found out that a college uses non-printable chars to do the same technique, for example:
String.Format(
"{1}{0}{2}{0}{3}{0}{4}",
(char)2,
myFirstString,
mySecondString,
myThirdString,
myFourthString);
and when we want to extract the hole string into it's parts
String.Split((char)2);
Is this safe? Should I adopt this way of safely splitting string? Is there any other safety technique?
It may be “safer” than the pipe because it is rarer, but both ways are suboptimal because they limit you to a subset of possible strings.
Consider using a proper encoding — one that unambiguously encodes a list of arbitrary strings. The simplest in terms of coding is probably to simply serialize a string[]
. You could use BinaryFormatter
or XmlSerializer
or something else.
If the result has to be a string, and it has to be a short one, then you could try something like this:
!
with !e
and every |
with !p
in every string. Now, none of the strings contains a |
and you can easily reverse this.|
as a separator.|
character.!p
with |
and !e
with !
in every string. This recovers the original strings.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With