remove new line in constant

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

How do I remove new line in constant if I have a string like this

string st1 ="\abc\def";

I use string trim but it doesn't work.

string st2;
for (int i = 0; i<st1.Lenght; i++)
{
st2 = st1.trim("\n".ToCharArray());
}

any idea?
thanks
 
ttan said:
How do I remove new line in constant if I have a string like this

string st1 ="\abc\def";

I use string trim but it doesn't work.

string st2;
for (int i = 0; i<st1.Lenght; i++)
{
st2 = st1.trim("\n".ToCharArray());
}

Trim only works on characters at the beginning and end of the string. You
want something like

st2 = st1.Replace("\n","");
 
ttan said:
How do I remove new line in constant if I have a string like this

string st1 ="\abc\def";

That's not a valid string - \d isn't a valid escape sequence. It also
doesn't contain any newlines...
I use string trim but it doesn't work.

string st2;
for (int i = 0; i<st1.Lenght; i++)
{
st2 = st1.trim("\n".ToCharArray());
}

any idea?

That won't compile either, for various reasons. It's also entirely
unclear what purpose you expect the loop to serve. In future, it would
be well worth you producing a short but complete example which can be
compiled so that people can see your problem more clearly. See
http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.

In this case, I believe your problem is that Trim only looks at the
start and end of the string. You should use Replace to replace all
occurrences.
 
Sorry for the unclear.

Problem is, I read in an mac address as an string "0040840c0290"
when I convert this into byte and it gave me 12 bytes by using :

byte [] buffer = Encoding.ASCII.GetBytes(this.Source.text);

But I'm only allow to store the mac address into 6 bytes.
[FieldOffset(0)] byte [] mac_add; //6 bytes
[FieldOffset(6)] byte [] ether_type; // 2 bytes
........
 
ttan said:
Sorry for the unclear.

Problem is, I read in an mac address as an string "0040840c0290"
when I convert this into byte and it gave me 12 bytes by using :

byte [] buffer = Encoding.ASCII.GetBytes(this.Source.text);

But I'm only allow to store the mac address into 6 bytes.
[FieldOffset(0)] byte [] mac_add; //6 bytes
[FieldOffset(6)] byte [] ether_type; // 2 bytes
.......

Well, it's not at all clear to me how that's related to your original
question, but it seems to me that you've got a hex string with 6 bytes
encoded there, which presumably needs to go in mac_add, and I'm not
sure about ether_type...
 
Back
Top