Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to IgnoreHeaderUnderscores #391

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 17 additions & 0 deletions src/CsvHelper.Tests/CsvReaderTests.cs
Expand Up @@ -700,6 +700,23 @@ public void SpacesInHeaderTest()
Assert.AreEqual( "one", data[0].StringColumn );
}

[TestMethod]
public void UnderscoresInHeaderTest()
{
var queue = new Queue<string[]>();
queue.Enqueue(new[] { "_Int_Column_", "_String_Column_" });
queue.Enqueue(new[] { "1", "one" });
queue.Enqueue(null);
var parserMock = new ParserMock(queue);
var reader = new CsvReader(parserMock);
reader.Configuration.IgnoreHeaderUnderscores = true;
var data = reader.GetRecords<TestDefaultValues>().ToList();
Assert.IsNotNull(data);
Assert.AreEqual(1, data.Count);
Assert.AreEqual(1, data[0].IntColumn);
Assert.AreEqual("one", data[0].StringColumn);
}

[TestMethod]
public void BooleanTypeConverterTest()
{
Expand Down
7 changes: 7 additions & 0 deletions src/CsvHelper/Configuration/CsvConfiguration.cs
Expand Up @@ -121,6 +121,13 @@ public virtual bool IsHeaderCaseSensitive
/// </summary>
public virtual bool IgnoreHeaderWhiteSpace { get; set; }

/// <summary>
/// Gets or sets a value indicating whether matcher header
/// column names will ignore underscores. True to ignore
/// underscores, otherwise false. Default is false.
/// </summary>
public virtual bool IgnoreHeaderUnderscores { get; set; }

/// <summary>
/// Gets or sets a value indicating whether references
/// should be ignored when auto mapping. True to ignore
Expand Down
6 changes: 6 additions & 0 deletions src/CsvHelper/CsvReader.cs
Expand Up @@ -1177,6 +1177,12 @@ protected virtual int GetFieldIndex( string[] names, int index = 0, bool isTryGe
foreach( var pair in namedIndexes )
{
var namedIndex = pair.Key;

if ( configuration.IgnoreHeaderUnderscores )
{
namedIndex = namedIndex.Replace( "_", string.Empty );
}

if( configuration.IgnoreHeaderWhiteSpace )
{
namedIndex = Regex.Replace( namedIndex, "\\s", string.Empty );
Expand Down