473,322 Members | 1,421 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 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 28153

"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 but the only read method requires you to know how...
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 keywords. So far I have: Function...
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 it? #include <vector> #include <string>...
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 log: PHP Notice: Use of undefined constant...
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 character by character and copying onto another...
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 Linux\Unix systems and "\r\n" on Windows systems ?...
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 that leaves empty tags in the markup. I need a...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.