Click here to Skip to main content
15,889,651 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
Hellow
From a text file ,i read 2 strings each one is in single line
C#
str1=sr.ReadLine();
str2=sr.ReadLine();

the text file is as below
scott
miller

i want to attache str2 to str1 and add a space between them
C#
str3=str1+ "   " + str2;

because of the "\r\n" in each string,i did not get whate i want,
"\r\n" must be removed
how can i subtract "\r\n" from both str1 and str2
Posted
Comments
ZurdoDev 14-Jan-15 12:59pm    
Did you try Replace? str1.Replace("\\r\\n", "");
PIEBALDconsult 14-Jan-15 13:13pm    
What? ReadLine will remove the line ending, so I don't know why you are asking.
Rob Philpott 14-Jan-15 15:35pm    
Good point!
Filipe Marques 14-Jan-15 13:17pm    
Hi, take a look at this: http://stackoverflow.com/questions/238002/replace-line-breaks-in-a-string-c-sharp Best regards, Filipe Marques

1 solution

If you only want to remove the \r\n, try Replace:
C#
str1 = sr.ReadLine().Replace("\r\n", String.Empty);
str2 = sr.ReadLine().Replace("\r\n", String.Empty);

If you want to remove all trailing whitespace (if there is any) including \r\n, you can use TrimEnd:
C#
str1 = sr.ReadLine().TrimEnd();
str2 = sr.ReadLine().TrimEnd();
 
Share this answer
 
v3
Comments
Sergey Alexandrovich Kryukov 14-Jan-15 17:10pm    
Sure, a 5.
(But it's always better to write string.Empty instead of ""...)
—SA
Thomas Daniels 15-Jan-15 11:18am    
Thanks! I also updated my answer to use String.Empty.
adriancs 16-Jan-15 1:33am    
using "" is creating an instance of string each time, but String.empty is not.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900