1
var file = from line in lines

select (((line => (line == ',' ? '.' : line)) || ((line => (line == ',' ? '.' : line))

How do I replace all ',' with '.' AND ';' with ', in C#

Is there any elegant way to do this in linq or do I have to it in two step something like below

var file1= from line in lines
           select (line.Replace(',', '.'));

var file2= from line2 in file1
           select (line2.Replace(';', ','));
1
  • 4
    Any reason you can't just chain two Replace calls? or use RegEx replace? Commented Nov 9, 2014 at 1:29

2 Answers 2

3

Replace returns a new string object so you can call any string method on the result, including Replace:

var file1= from line in lines
           select line.Replace(',', '.')
                      .Replace(';', ',')
Sign up to request clarification or add additional context in comments.

Comments

3

I'd use method syntax. They're completely interchangeable, but LINQ query syntax looks strange here:

var file1 = lines.Select(l => l.Replace(',', '.').Replace(';', ','));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.