Solved

Converting string to initial caps: my_field_name

Posted on 2012-04-10
5
863 Views
Last Modified: 2012-04-10
I'm retrieving a fieldname from a table and want to convert it to initial caps and remove the underscore like so:

my_field_name
to
MyFieldName

How do I do that? I'm using vb.net but C# is fine if that is all you know.

thanks!
0
Comment
Question by:Starr Duskk
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 38

Accepted Solution

by:
PaulHews earned 500 total points
ID: 37829334
Try this function:
    Private Function ConvertFieldName(Name As String) As String
        Return StrConv(Name.Replace("_", " "), VbStrConv.ProperCase).Replace(" ", String.Empty)
    End Function

Open in new window

0
 
LVL 17

Expert Comment

by:nepaluz
ID: 37829403
or even
Private Function ConvertFieldName(Name As String) As String
    Return StrConv(Name.Replace("_", String.Empty), VbStrConv.ProperCase)
End Function

Open in new window

0
 
LVL 38

Expert Comment

by:PaulHews
ID: 37829421
nepaluz,

Not quite as that returns "Myfieldname" not "MyFieldName"
0
 
LVL 20

Expert Comment

by:BuggyCoder
ID: 37829462
Here is the code for you:-

private static string ToTitleCase(string text, char splitCharacter)
        {
            var txtInfo = new CultureInfo("en-US", false).TextInfo;
            var builder = new StringBuilder();

            text.Split(splitCharacter).ToList().ForEach(s => builder.Append(txtInfo.ToTitleCase(s)));
            return builder.ToString();
        }

Open in new window


Here is how to use this:-

string test = "my_field_name";
ToTitleCase(test,'_');

Open in new window

Add the following namespace:-
using System.Globalization;
0
 
LVL 2

Author Closing Comment

by:Starr Duskk
ID: 37829583
Perfect!
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question