2

I have a class:

MyClass
{
   public int Id{set;get;}
   public string MollName{set;get;}
   public string MollAddress{set;get;}
   public string MollPRG{set;get;}
}

i use it for generate report in Excel. I makw a whole document programaticaly so i no have any templates.
For make a report's columns names i use:

var fields = typeof(MyClass).GetFields(
            BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
var names = Array.ConvertAll(fields, field => field.Name).ToList();

var trimedNames = names.Select(name => name.Substring(1, name.IndexOf(">", StringComparison.Ordinal) - 1)).ToList();
foreach (var fieldName in trimedNames)
{
//**some code
}

And i get report with column names:

Id
MollName
MollAddress
MollPRG

How can i get it with spaces?:

Id
Moll Name
Moll Address
Moll PRG
Kliver Max
  • 5,107
  • 22
  • 95
  • 148
  • See http://stackoverflow.com/questions/155303/net-how-can-you-split-a-caps-delimited-string-into-an-array – haim770 Mar 18 '15 at 12:52
  • You could do a regex search and replace to match capital letters, not at the beginning of the string, and not followed by capital letters or at the end of the string. – Brian Warshaw Mar 18 '15 at 12:52

1 Answers1

3

You could try a regex, for sample:

foreach (var fieldName in trimedNames)
{
    var fieldNameWithSpaces = Regex.Replace(fieldName, "(\\B[A-Z])", " $1");

    // you can use fieldNameWithSpaces here...
}

See this post: .NET - How can you split a "caps" delimited string into an array?

Community
  • 1
  • 1
Felipe Oriani
  • 37,948
  • 19
  • 131
  • 194