public static string[] CleanFieldNames(string[] fields)
{ string[] output;
int len = fields.Length;
string exprStartsWithDigit = @"^\d";
List<string> list = new List<string>();
for (int i = 0; i < len; i++)
{ string field = fields[i];
// Replace spaces with underscores
field = field.Replace(" ", "_"); // Replace hyphens with underscores
field = field.Replace("-", "_"); // Precede digit at start of field name with underscore
if (Regex.IsMatch(field, exprStartsWithDigit))
{ field = string.Concat("_", field); }
// Replace extended chars with non-diacritic equivalent
field = RemoveDiacritics(field);
list.Add(field);
}
output = new string[list.Count];
list.CopyTo(output);
return output;
}
public static string RemoveDiacritics(string input)
{ string stFormD = input.Normalize(NormalizationForm.FormD);
int len = stFormD.Length;
StringBuilder sb = new StringBuilder();
for (int i = 0; i < len; i++)
{ UnicodeCategory uc = CharUnicodeInfo.GetUnicodeCategory(stFormD[i]);
if (uc != UnicodeCategory.NonSpacingMark)
{ sb.Append(stFormD[i]);
}
}
return (sb.ToString().Normalize(NormalizationForm.FormC));
}