4

We store ContentDelimiter config (which we use to delimite the content) in Database as string (which could be "tab", i.e. \t , or new line \r\n)

Later on we would like to use this config, how would I go about converting \t (which is string, not chat) to tab char ?

Example:

string delimiterConfig =  config.GetDelimiter();
char[] delimiter = ConvertConfig(delimiterConfig);

How would ConvertConfig will look like so that it will parse all escaped strings back into chars so that "\t" string will become \tchar.

Any elegant solutions without using case statements and replace ?

Bek Raupov
  • 3,782
  • 3
  • 24
  • 42
  • You could create a bit of code on the fly, compile and run it. A switch/case or a SortedDictionary would be much simpler. – Skizz Jul 18 '11 at 16:15
  • yes, but that means you need to have case for each escaped scenario. quite suprised that .NET doesnt have extension for it – Bek Raupov Jul 18 '11 at 16:23

5 Answers5

4

If by "better" solution, you mean faster:

static String Replace(String input)
    {
        if (input.Length <= 1) return input;

        // the input string can only get shorter
        // so init the buffer so we won't have to reallocate later
        char[] buffer = new char[input.Length];
        int outIdx = 0;
        for (int i = 0; i < input.Length; i++)
        {
            char c = input[i];
            if (c == '\\')
            {
                if (i < input.Length - 1)
                {
                    switch (input[i + 1])
                    {
                        case 'n':
                            buffer[outIdx++] = '\n';
                            i++;
                            continue;
                        case 'r':
                            buffer[outIdx++] = '\r';
                            i++;
                            continue;
                        case 't':
                            buffer[outIdx++] = '\t';
                            i++;
                            continue;
                    }
                }
            }

            buffer[outIdx++] = c;
        }

        return new String(buffer, 0, outIdx);
    }

This is significantly faster than using Regex. Especially when I tested against this input:

var input = new String('\\', 0x1000);

If by "better" you mean easier to read and maintain, then the Regex solution probably wins. There might also be bugs in my solution; I didn't test it very thoroughly.

MarkPflug
  • 28,292
  • 8
  • 46
  • 54
3

Here's an elegant solution with a switch statement, the Regex.Replace Method and a custom MatchEvaluator:

var input = @"This is indented:\r\n\tHello World";

var output = Regex.Replace(input, @"\\[rnt]", m =>
{
    switch (m.Value)
    {
    case @"\r": return "\r";
    case @"\n": return "\n";
    case @"\t": return "\t";
    default: return m.Value;
    }
});

Console.WriteLine(output);

Output:

This is indented:
        Hello World
stema
  • 90,351
  • 20
  • 107
  • 135
dtb
  • 213,145
  • 36
  • 401
  • 431
  • Jon Skeet mentioned similar solution in similar post, was wondering if there is better solution than this – Bek Raupov Jul 18 '11 at 16:22
  • Well, you have a string and you want to replace parts of it and have multiple cases. So any solution will be "using case statements and replace" in one form or another, because that's exactly what you need. The question is: do you have to implement it yourself or is there any method in the .NET Framework that does it for you. The answer is: you have to implement it yourself (or copy a code snippet off the web). – dtb Jul 18 '11 at 16:29
  • "was wondering if there is better solution than this" Define better. – MarkPflug Jul 18 '11 at 16:38
  • yeah, when i said, i meant something built in – Bek Raupov Jul 19 '11 at 11:25
2

For the limited set of basic ASCII delimiters you also have a simple solution:

Regex.Unescape(input)

You can read all about it in the MSDN documentation, but basically it works with all of the Regex delimiters and whitespace literals.

Be aware that it throws on unknown escape sequences.

glopes
  • 4,038
  • 3
  • 26
  • 29
1

If by better, you were referring to the lack escape sequences supported, then I suggest you check out my response to the question titled: Evaluate escaped string which handles standard escape sequences, octal escape sequences, and Unicode escape sequences. I hope you find this solution to be more elegant and fitting to your needs.

Community
  • 1
  • 1
deAtog
  • 61
  • 3
0

What about ToCharArray method?

string x = "\r\n";
char[] delimeter = x.ToCharArray();
Vlad Bezden
  • 83,883
  • 25
  • 248
  • 179