5

I'm stuck. Where can I add "Trim" to this C# statement? Each line will be a separate file name. But because each file may have space after it, I'd like to trim it off.

string[] lines = (File.ReadAllLines(@"C:\Users\Jim\Desktop\adminkeys.cfg")); 

Thanks

1
  • Hum, maybe (File.ReadAllLines(@"C:\Users\Jim\Desktop\adminkeys.cfg")).Select(s=> s.Trim()); Commented Nov 30, 2012 at 19:39

1 Answer 1

12

If you are trying to trim each line, then you need to do it line by line, for example, with LINQ:

string[] lines = (File.ReadAllLines(@"C:\Users\Jim\Desktop\adminkeys.cfg")) .Select(l => l.Trim()).ToArray(); 
Sign up to request clarification or add additional context in comments.

1 Comment

For .NET 4.0, it would be slightly more efficient to replace ReadAllLines with ReadLines: msdn.microsoft.com/en-us/library/dd383503.aspx

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.