Alex Rivera | Logout

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

Asked 2009-05-02T22:32:51.350
38

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.

Edit
Report

3 Answers

7

The best solution is to stick to commas and introduce support for character escaping. Whatever character you select will eventually need to be entered so you may aswell provide support for this.

Think backslases + double quotes inside double quoted strings.

Don't pick a character like backtick because some users might not know how to type it in...

answered 2009-05-02T22:41:17.303
5

Will the user be entering delimited strings into the textboxes, or will they be entering individual strings which will then be built into delimited strings by your code?

In the first case it might be better to rethink your UI instead. eg, The user could enter one string at a time into a textbox and click an "Add to list" button after each one.

In the second case it doesn't really matter what delimiter you use. Choose any character you like, just ensure that you escape any other occurrences of that character.

EDIT

Since several comments on other answers are asking for code, here's a method to create a comma-delimited string, using backslash as the escape character:

public static string CreateDelimitedString(IEnumerable<string> items)
{
    StringBuilder sb = new StringBuilder();

    foreach (string item in items)
    {
        sb.Append(item.Replace("\\", "\\\\").Replace(",", "\\,"));
        sb.Append(",");
    }

    return (sb.Length > 0) ? sb.ToString(0, sb.Length - 1) : string.Empty;
}

And here's the method to convert that comma-delimited string back to a collection of individual strings:

public static IEnumerable<string> GetItemsFromDelimitedString(string s)
{
    bool escaped = false;
    StringBuilder sb = new StringBuilder();

    foreach (char c in s)
    {
        if ((c == '\\') && !escaped)
        {
            escaped = true;
        }
        else if ((c == ',') && !escaped)
        {
            yield return sb.ToString();
            sb.Remove(0, sb.Length);
        }
        else
        {
            sb.Append(c);
            escaped = false;
        }
    }

    yield return sb.ToString();
}

And here's some example usage:

string[] test =
    {
        "no commas or backslashes",
        "just one, comma",
        @"a comma, and a\ backslash",
        @"lots, of\ commas,\ and\, bac
answered 2009-05-02T22:48:25.697
0

Newline? (i.e. use a multi-line text box)

answered 2009-05-02T22:36:50.967

Your Answer