472,328 Members | 1,107 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,328 software developers and data experts.

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 28099

"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Amy L. | last post by:
Is there a way through .net to read a very large text file (400MB+) backwards line by line. In system.io the filestream class has a "seek" method...
4
by: Simon Harris | last post by:
Hi All, I am trying to write a function that will remove all carriage returns from a string, so that the string of words can be used as meta...
11
by: lovecreatesbeauty | last post by:
Hello experts, Is const_cast only applied to pointers or references? If I have a constant object, then how can I remove constant attribute from...
2
by: collinm | last post by:
hi here my code FILE *fp; char *line; #define LINE_MAX 30 fp = fopen("test1.txt", "r");
3
by: Chakkaradeep | last post by:
Hi all, i have a string, str_old="E:\a\b\c.exe", and i want to change to str_new="E:\\a\\b\\c.exe".
2
by: gavino | last post by:
http://hashphp.org/pastebin.php?format=plain&pid=8181 the $txt line here is giving me problems along with the two if lines below complaint from...
15
by: DanielJohnson | last post by:
I am writing a program in which I am removing all the spaces from the string. I thought that I could do it two ways. One was parsing the string...
21
by: Timothy Madden | last post by:
Hello The examples in the php cli chapter in the manual end a line with "\n". For example echo "Total {$total}.\n"; Should it not be "\n" on...
11
by: David | last post by:
Hi All, I am working on a script that is theoreticaly simple but I can not get it to work completely. I am dealing with a page spit out by .NET...
0
by: tammygombez | last post by:
Hey fellow JavaFX developers, I'm currently working on a project that involves using a ComboBox in JavaFX, and I've run into a bit of an issue....
0
by: tammygombez | last post by:
Hey everyone! I've been researching gaming laptops lately, and I must say, they can get pretty expensive. However, I've come across some great...
0
by: concettolabs | last post by:
In today's business world, businesses are increasingly turning to PowerApps to develop custom business applications. PowerApps is a powerful tool...
0
better678
by: better678 | last post by:
Question: Discuss your understanding of the Java platform. Is the statement "Java is interpreted" correct? Answer: Java is an object-oriented...
0
by: teenabhardwaj | last post by:
How would one discover a valid source for learning news, comfort, and help for engineering designs? Covering through piles of books takes a lot of...
0
by: CD Tom | last post by:
This happens in runtime 2013 and 2016. When a report is run and then closed a toolbar shows up and the only way to get it to go away is to right...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
1
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.