I have several textboxes where users can enter information into them. This can include commas, so I can't use the standard comma delimited strings.
What is a good delimiter to denote that strings should be separated based on that character that isn't typically used by users in their writings? I'm going to be combining these fields into a string string and passing them off to my Encryption method I have. After I decrypt them I need to be able to reliably separate them.
I'm using C# if it matters.
Delimited formats Any character may be used to separate the values, but the most common delimiters are the comma, tab, and colon. The vertical bar (also referred to as pipe) and space are also sometimes used.
CSV and importing into most programs is usually not a problem. Just specify TAB delimited rather than comma when you import your file. If there are commas in your data you WILL have a problem when specifying comma delimited as you are well aware.
A delimiter is one or more characters that separate text strings. Common delimiters are commas (,), semicolon (;), quotes ( ", ' ), braces ({}), pipes (|), or slashes ( / \ ). When a program stores sequential or tabular data, it delimits each item of data with a predefined character.
| would be next on my list and is often used as an alternative to CSV. google "pipe delimited" and you will find many examples.
string[] items = new string[] {"Uno","Dos","Tres"}; string toEncrypt = String.Join("|", items); items = toEncrypt.Split(new char[] {'|'}, StringSplitOptions.RemoveEmptyEntries); foreach(string s in items) Console.WriteLine(s);
And since everyone likes to be a critic about the encoding and not provide the code, here is one way to encode the text so your | delim won't collide.
string[] items = new string[] {"Uno","Dos","Tres"}; for (int i = 0; i < items.Length; i++) items[i] = Convert.ToBase64String(Encoding.UTF8.GetBytes(items[i])); string toEncrypt = String.Join("|", items); items = toEncrypt.Split(new char[] {'|'}, StringSplitOptions.RemoveEmptyEntries); foreach (string s in items) Console.WriteLine(Encoding.UTF8.GetString(Convert.FromBase64String(s)));
I have seen unusal characters used as delimiters, even unusal character combinarions like -|::|-
, but eventhough they are more unlikely to occur, they still can.
You have basically two options if you want to make it water tight:
1: Use a character that is impossible to type, like the '\0' character:
Join:
string combined = string.Join("\0", inputArray);
Split:
string[] result = combined.Split('\0');
2: Escape the string and use an escaped character as delimiter, like url encoding the values and use & as delimiter:
Join:
string combined = string.Join("&", inputArray.Select<string,string>(System.Web.HttpUtility.UrlEncode).ToArray());
Split:
string[] result = combined.Split('&').Select<string,string>(System.Web.HttpUtility.UrlDecode).ToArray();
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