Skip to content
This repository was archived by the owner on Dec 24, 2022. It is now read-only.

Commit 8e83147

Browse files
committed
Auto trim headers with leading/trailing whitespace
1 parent 6b32fb4 commit 8e83147

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

src/ServiceStack.Text/CsvReader.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,8 @@ public static List<string> ParseLines(string csv)
5959
return rows;
6060
}
6161

62-
public static List<string> ParseFields(string line)
62+
public static List<string> ParseFields(string line) => ParseFields(line, null);
63+
public static List<string> ParseFields(string line, Func<string,string> parseFn)
6364
{
6465
var to = new List<string>();
6566
if (string.IsNullOrEmpty(line))
@@ -70,7 +71,7 @@ public static List<string> ParseFields(string line)
7071
while (++i <= len)
7172
{
7273
var value = EatValue(line, ref i);
73-
to.Add(value.FromCsvField());
74+
to.Add(parseFn != null ? parseFn(value.FromCsvField()) : value.FromCsvField());
7475
}
7576

7677
return to;
@@ -355,7 +356,9 @@ public static List<T> Read(List<string> rows)
355356

356357
List<string> headers = null;
357358
if (!CsvConfig<T>.OmitHeaders || Headers.Count == 0)
358-
headers = CsvReader.ParseFields(rows[0]);
359+
{
360+
headers = CsvReader.ParseFields(rows[0], s => s.Trim());
361+
}
359362

360363
if (typeof(T).IsValueType || typeof(T) == typeof(string))
361364
{

0 commit comments

Comments
 (0)