tags:

views:

234

answers:

6

I am pulling data from a database that uses ascii character 254 as a delimiter. I want to replace that with a new line.

I tried this:

rec = rec.Replace(char(254), Environment.NewLine);

This isn't working though.

+1  A: 

It seems like your char assignment is wrong:

rec = rec.Replace((char)254, Environment.NewLine);

just realized there's a type mismatch on that line, too. Here's the code without the mismatch:

rec = rec.Replace("" + (char)254, Environment.NewLine);
jball
+1  A: 

Char() is not a constructor for characters. You need to cast a number as a character, there is no equivalent to the VB.NET Chr() function in C#.

Try something like this:

rec = rec.Replace( (String) ((char) 254), Environment.NewLine);

Edit: String.Replace does not have an overload for (char,string) and NewLine can be multiple characters, so I added another cast to string just to be explicit. (Gonzalo, you caught me during my edit! lol)

richardtallent
Environment.NewLine is a string and there's no Replace (char, string) overload.
Gonzalo
+6  A: 

Use this:

rec = rec.Replace ('\xFE', '\n');

or this:

rec = rec.Replace ("\xFE", Environment.NewLine);
Gonzalo
Initially I didn't like this answer, but then remembered that the \xFE should be translated to the right character at compile-time, so it is the cleanest answer. I would stay away from the simple LF character replacement, though, and stick with Environment.NewLine unless you specifically need a literal \x0A character.
richardtallent
A: 

Assuming what you have won't compile, try this:

rec = rec.Replace( ((char)254).ToString(), Environment.NewLine);
Austin Salonen
+1  A: 

Environment.NewLine is a string... you can only replace a char with a char, or a string with a string.

You could try this:

rec = rec.Replace("" + (char)254, Environment.NewLine);
ThePilot
+1  A: 

"" + (char)254 is inelegant. Use "\xFE" instead.

Seva Alekseyev