2

This is my situation, I was able to remove the spaces using trim or replace. But it does not remove the linefeed or newline at the end of each file. How can I remove the spaces and linefeed in C#?

Thanks in advance.

user55700
  • 31
  • 1
  • 4
  • Do you have the file in a string buffer? And do you want to remove whitespace and newlines/linefeed characters at the end of the file or at the end of every line? – Dirk Vollmar Jan 23 '09 at 07:16
  • It would *really* help to see your code. It's pretty much impossible to diagnose what's wrong without seeing it. – Jon Skeet Jan 23 '09 at 07:19

2 Answers2

6

You can call Trim method with all characters that you want to be removed like:

line = line.Trim(' ','\r','\n');
idursun
  • 6,261
  • 1
  • 37
  • 51
0
myString = myString.Replace(System.Environment.NewLine, string.Empty)
myString = myString.Replace(" ", string.Empty);

Alternately, use:

string replacement = Regex.Replace(s, @" |\t|\n|\r", string.Empty);

For Line Feeds and New Lines:

myString = myString.Replace(System.Environment.NewLine, "replacement text")

For Spaces:

myString = myString.Replace(" ", "replacement text");

Reference:

  1. StackOverFlow Link - System.Environment.NewLine
  2. StackOverFlow Link - Regex
user3613932
  • 1,219
  • 15
  • 16