how to replace linefeed with empty string in C#?

G

Guest

Hi there,

Here is the line of VB code and I would like to translate into C#, please
help! Thanks!

sMsgIntHeader = (Replace(Replace(Trim(Left(sErrorMsg, InStr(5, sErrorMsg,
vbCrLf))), vbLf, ""), vbCr, ""))
 
C

Chuck

Wow.. that is pretty ugly.. I would try to make it easier to read in the
conversion to C# if I were you.

Here are some tips to get you started...

Take a look at the String class in .NET help. It contains all the helper
functions that you mentioned (Replace, Trim, Left, InStr, etc.) but they may
have a little different names.

In C# you can represent vbCr as "\r" and vbLf as "\n" so a replace may look
something like:
myStringVariable.Replace("\r", " ")

If there will always be a CR and LF at the end of your string then you could
do something like:
myStringVariable.Replace("\r\n", " ")

Hope these help!
Chuck
 
S

Steve Walker

cloudx said:
Hi there,

Here is the line of VB code and I would like to translate into C#, please
help! Thanks!

sMsgIntHeader = (Replace(Replace(Trim(Left(sErrorMsg, InStr(5, sErrorMsg,
vbCrLf))), vbLf, ""), vbCr, ""))

You could use something along the lines of

string s = "a,b,.c,d/e.f";
string [] t = s.Split('.',',','/');
string u = string.Concat(t);

replacing '.', ',','/' with whatever characters you want to remove. Not
particularly fast or efficient, but if all you are doing is cleaning up
a short message, it doesn't really matter.

Or use the static System.Text.RegularExpressions.Regex.Replace method
using an appropriate pattern.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top