473,402 Members | 2,061 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,402 software developers and data experts.

Take away "\r\n" from end of String

Hello!
I am making a program where it reads out a TXT file like this...

string line=sr.ReadLine();
textBox1.Text+=line += "\r\n";

The problem with this code is that, each time it loads a file, a unneeded
"\r\n" is added to the end of the text.

So, I would like know how to take away "\r\n" from the end of a string.

Thanks in advance!
Nov 15 '05 #1
4 5472
You should use Environment.NewLine
"Tomomichi Amano" <to*********@hotmail.com> wrote in message
news:#a**************@TK2MSFTNGP12.phx.gbl...
Hello!
I am making a program where it reads out a TXT file like this...

string line=sr.ReadLine();
textBox1.Text+=line += "\r\n";

The problem with this code is that, each time it loads a file, a unneeded
"\r\n" is added to the end of the text.

So, I would like know how to take away "\r\n" from the end of a string.

Thanks in advance!

Nov 15 '05 #2
Hi,

I would reverse the logic by adding newline beforhand:
textBox1.Text+= (textBox1.Text.Length>0 ? "\r\n": string.Empty) + line;

--
Miha Markic - RightHand .NET consulting & software development
miha at rthand com
www.rthand.com
"Tomomichi Amano" <to*********@hotmail.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
Hello!
I am making a program where it reads out a TXT file like this...

string line=sr.ReadLine();
textBox1.Text+=line += "\r\n";

The problem with this code is that, each time it loads a file, a unneeded
"\r\n" is added to the end of the text.

So, I would like know how to take away "\r\n" from the end of a string.

Thanks in advance!

Nov 15 '05 #3
Tomomichi Amano <to*********@hotmail.com> wrote:
Hello!
I am making a program where it reads out a TXT file like this...

string line=sr.ReadLine();
textBox1.Text+=line += "\r\n";
That code looks relatively unreadable to me - why are you using +=
instead of just + on the second part? It's easier (IMO) to read:

textBox1.Test += line+"\r\n";
The problem with this code is that, each time it loads a file, a unneeded
"\r\n" is added to the end of the text.

So, I would like know how to take away "\r\n" from the end of a string.


Just take a substring which doesn't include the last two characters:

string x = x.Substring(0, x.Length-2);

However, the above will be very inefficient, particularly if the file
is long. It would be better to use a StringBuilder to build up the
whole string:

string line = sr.ReadLine();
stringBuilder.Append(line);
stringBuilder.Append("\r\n");

and *then* convert it to a string at the end.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #4
String.TrimEnd() might do the trick.
-mike
MVP

"Tomomichi Amano" <to*********@hotmail.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
Hello!
I am making a program where it reads out a TXT file like this...

string line=sr.ReadLine();
textBox1.Text+=line += "\r\n";

The problem with this code is that, each time it loads a file, a unneeded
"\r\n" is added to the end of the text.

So, I would like know how to take away "\r\n" from the end of a string.

Thanks in advance!


Nov 15 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: BJörn Lindqvist | last post by:
I think it would be cool if you could refer to instance variables without prefixing with "self." I know noone else thinks like me so Python will never be changed, but maybe you can already do it...
2
by: Alexander Bartzas | last post by:
I'm trying to compile route.cpp part of the NetBench Suite (UCLA) and...: > g++ packet.cpp radix.cpp route.cpp -o route There is no problem in the compilation of the code but when I try to run...
10
by: KENNY L. CHEN | last post by:
Dear experts, I have two tables in my Oracle 8i database: TEST (COL1,COl2,REC_NO) and TEST1 (COL1,COL2,REC_NO). Both tables are unique-indexed on (COL1,COL2,REC_NO). I think the following...
6
by: Erik R.N. | last post by:
Hi I have a ACCESS2002 table with a date-field in format "dd-mm-yyyy" which I "export" to a txt-file ( ; and " " ) and upload the txt-file to a MYSQL-table where the datefield is a varchar-field....
235
by: napi | last post by:
I think you would agree with me that a C compiler that directly produces Java Byte Code to be run on any JVM is something that is missing to software programmers so far. With such a tool one could...
5
by: SenthilSS | last post by:
My application produces XML Data files which have XML namespace qualified XML elements (nodes), but the namespace itself is not declared in the data file. My task is to read these data files in a...
1
by: Bernd Hohmann | last post by:
I have a question about using "*" in (sub)queries. This example query counts the number of records for a certain customer starting with record "80172" and stops after 10 rows. SELECT...
2
by: SharpCoderMP | last post by:
hi, i'm writing a console application and i would like to be able to safely retrive password from the user, so noone can see how it is being typed. consider this scenario: Console.Write("Enter...
232
by: robert maas, see http://tinyurl.com/uh3t | last post by:
I'm working on examples of programming in several languages, all (except PHP) running under CGI so that I can show both the source files and the actually running of the examples online. The first...
5
matheussousuke
by: matheussousuke | last post by:
Hello, I'm using tiny MCE plugin on my oscommerce and it is inserting my website URL when I use insert image function in the emails. The goal is: Make it send the email with the URL...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.