Jedhi
Jedhi

Reputation: 73

Replacing characters in a string array with linq

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(';', ','));

Upvotes: 1

Views: 3808

Answers (2)

Jonesopolis
Jonesopolis

Reputation: 25370

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

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

Upvotes: 3

Selman Genç
Selman Genç

Reputation: 101681

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(';', ',')

Upvotes: 3

Related Questions