views:

56

answers:

3

I will read a file from my computer using

   StreamReader sr = new StreamReader(FileName);

            string str = sr.ReadToEnd();

In this i am getting some illegal characters like /n,/r and some other.

I Would like to replace illegal characters with a empty character. I tried of making an character array but i did not able to remove those so can any one help me

+3  A: 

You can use the String.Replace method:

string str = sr.ReadToEnd().Replace("\r", "").Replace("\n", "");

However it's not a very good idea if the string is long and you have a long list of illegal characters, because each call to Replace will create a new instance of String. A better option would be to filter out the illegal characters using Linq :

char[] illegalChars = new[] { '\r', '\n' }; // add other illegal chars if needed
char[] chars = sr.ReadToEnd().Where(c => !illegalChars.Contains(c)).ToArray();
string str = new String(chars);

However the call to Contains adds overhead, it is faster to test directly against each illegal character:

char[] chars = sr.ReadToEnd().Where(c => c != '\r' && c != '\n').ToArray();
string str = new String(chars);

And for completeness, here's an even faster version:

StringBuilder sb = new StringBuilder();
foreach(char c in sr.ReadToEnd())
{
    if (c != '\r' && c != '\n')
        sb.Append(c);
}
string str = sb.ToString();
Thomas Levesque
@Developer Art, I know, I was already editing my answer
Thomas Levesque
That looks very creative but still the performance with LINQing will suffer.
Developer Art
sr.readtoend().where this doesnot exists na then how can i call this
Dorababu
@Dorababu, just add a `using System.Linq` clause in your file (but you won't see Where in the intellisense because it's hidden for strings). @Developer Art, Linq doesn't make it slower... it's basically just a foreach loop with a test in it, Linq doesn't add that much overhead. However the call to `Contains` made it a bit slow, so I posted a new version that is actually faster than yours ;). The problem with `StringBuilder.Replace` is that it has to move big chunks of data each time it removes a character
Thomas Levesque
I am using in 2.0 is there any other way
Dorababu
@Dorababu, yes, see my last edit. The last code doesn't use Linq
Thomas Levesque
Ok thanks you all. All 3 worked so i don't know whom to mark as Answer
Dorababu
Run some tests and pick the fastest ;)
Thomas Levesque
+1 for all your efforts.
Developer Art
@Developer Art, thanks :)
Thomas Levesque
+3  A: 
StreamReader sr = new StreamReader (FileName);

StringBuilder sb = new StringBuilder (sr.ReadToEnd());

sb.Replace ("\r\n", String.Empty);
sb.Replace ("\n", String.Empty);

string hereIsYourString = sb.ToString ();
Developer Art
You should use `String.Empty` instead of `""`
Scoregraphic
Yes, agree, that will look cleaner.
Developer Art
The problem with Replace is that the StringBuilder constantly moves chars in its buffer. It is much faster to pre-filter the characters and add them to the StringBuilder one by one
Thomas Levesque
+2  A: 
string str = string.Join(string.Empty, File.ReadAllLines(FileName));
Darin Dimitrov
This sounds good. Can you explain how it is removing all those illegal characters
Dorababu
@Dorababu, all that this does is stripping new lines (\r\n) from a file. I don't know what you mean by illegal characters as new lines are not illegal characters but I had the impression that you were looking for stripping \r\n from a file so I proposed this solution.
Darin Dimitrov
@Darin ok thanks for the solution
Dorababu