469,929 Members | 1,413 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,929 developers. It's quick & easy.

remove new line in constant


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

Nov 17 '05 #1
5 27976

"ttan" <tt**@discussions.microsoft.com> wrote in message
news:13**********************************@microsof t.com...

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","");
Nov 17 '05 #2
ttan <tt**@discussions.microsoft.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #3
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
........
"Jon Skeet [C# MVP]" wrote:
ttan <tt**@discussions.microsoft.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 17 '05 #4
string st1 ="abc\r\ndef";
string st2 = st1.Replace("\r\n\", "");

HTH

"ttan" <tt**@discussions.microsoft.com> wrote in message
news:13**********************************@microsof t.com...

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

Nov 17 '05 #5
ttan <tt**@discussions.microsoft.com> wrote:
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...

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Simon Harris | last post: by
11 posts views Thread by lovecreatesbeauty | last post: by
2 posts views Thread by collinm | last post: by
3 posts views Thread by Chakkaradeep | last post: by
2 posts views Thread by gavino | last post: by
15 posts views Thread by DanielJohnson | last post: by
21 posts views Thread by Timothy Madden | last post: by
11 posts views Thread by David | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.