Question: I am new to C# and trying to lean how to filter data that I read from a file. I have a file that I read from that has data similer to the follwoing:

```
3 286 858 95.333 0.406 0.427 87.00 348 366 4 b
9 23 207 2.556 0.300 1.00 1.51 62 207 41 a
9 37 333 4.111 0.390 0.811 2.03 130 270 64 a
10 21 210 2.100 0.348 0.757 3.17 73 159 23 a
9 79 711 8.778 0.343 0.899 2.20 244 639 111 a
10 66 660 6.600 0.324 0.780 2.25 214 515 95 a

```

When I read these data, some of them have `Carriage return` Or `Line Feed` characters hidden in them. Can you please tell me if there is a way to remove them. For example, one of my variable may hold the the following value due to a newline character in them:

```
mystringval = "9
               "

```

I want this `mystringval` variable to be converted back to

```
mystringval  = "9"

```

Answer: 
If you want to get rid of all special characters, you can learn regular expressions and use [Regex.Replace](https://msdn.microsoft.com/en-us/library/system.text.regularexpressions.regex.replace(v=vs.110).aspx).

```
var value = "&*^)#abcd.";
var filtered = System.Text.RegularExpressions.Regex.Replace(value, @"[^\w]", "");

```

**REGEXPLANATION**

* the `@` before the string means that you're using a literal string and c# escape sequences don't work, leaving only the regex escape sequences
* `[^abc]` matches all characters that are not a, b, or c(to replace them with empty space)
* `\w` is a special regex code that means a letter, number, or underscore
* you can also use `@"[^A-Za-z0-9\.]"` which will filter letters, numbers and decimal. See <http://rubular.com/> for more details.
As well as using RegEx, you can use LINQ to do something like

```
var goodCharacters = input
    .Replace("\r", " ")
    .Replace("\n", " ")
    .Where(c => char.IsLetterOrDigit(c) || c == ' ' || c == '.')
    .ToArray();
var result = new string(goodCharacters).Trim();

```

The first two `Replace` calls will guard against having a number at the end of one line and a number at the start of the next, e.g. `"123\r\n987"` would otherwise be `"123987"`, whereas I assume you want `"123 987"`.

Try my sample [here on ideone.com](http://ideone.com/RY6y96).