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 make a whole document programmatically so i don't 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
2
  • See stackoverflow.com/questions/155303/… Commented Mar 18, 2015 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. Commented Mar 18, 2015 at 12:52

1 Answer 1

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?

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.