Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is a more unique delimiter than comma for separating strings?

Tags:

c#

csv

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.

like image 325
KingNestor Avatar asked May 02 '09 22:05

KingNestor


People also ask

What is the most common delimiter?

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.

What is the best delimiter for CSV?

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.

What can I use as a delimiter?

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.


2 Answers

| 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))); 
like image 195
Chad Grant Avatar answered Sep 24 '22 07:09

Chad Grant


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(); 
like image 39
Guffa Avatar answered Sep 23 '22 07:09

Guffa