Skip to main content
  1. About
  2. For Teams
Asked
Viewed 2k times
2

Given this string: "EMAIL_LOG"

I want to convert it to: EmailLog

I have this code that does do the job:

private static string TitleCaseConvert(string title)
{
    CultureInfo cultureInfo = Thread.CurrentThread.CurrentCulture;
    TextInfo textInfo = cultureInfo.TextInfo;
    title = title.Replace("_", " ").ToLower();
    title = textInfo.ToTitleCase(title);
    title = title.Replace(" ", "");
    return title;
}

I was just wondering if there was a better suggestion to do this conversion or one that was more elegant perhaps?

Thanks.

2
  • 1
    seems legit. I just dont understand this line : CultureInfo cultureInfo = Thread.CurrentThread.CurrentCulture; why do we need to talk to the currenthread.currentculture. why isn't it a singleton ?
    Christophe Chenel
    –  Christophe Chenel
    2018-11-15 21:28:39 +00:00
    Commented Nov 15, 2018 at 21:28
  • 1
    .TextInfo.ToTitleCase("EMAIL_LOG".ToLower()).Replace("_", "")
    Slai
    –  Slai
    2018-11-15 21:51:16 +00:00
    Commented Nov 15, 2018 at 21:51

6 Answers 6

2

What about doing it in one statement, like this:

private static string TitleCaseConvert(string title)
{ 
   return new CultureInfo("en").TextInfo.ToTitleCase(title.ToLower().Replace("_", " ")).Replace(" ", "");
}
Sign up to request clarification or add additional context in comments.

1 Comment

This is what I am after, thanks. I added a second var in the call for a separator and defaulted it to "_". This gives me the flexibility to pass in whatever separator I need to do this Rename.
2

You could use Split() to split on the _ characters and StringBuilder to reconstruct the output string. This should perform better because you are using StringBuilder instead of constructing new strings everytime:

private static string ToPascalCase(string input)
{
    if(input == null)
    {
        throw new ArgumentNullException("input");
    }

    TextInfo textInfo = CultureInfo.CurrentCulture.TextInfo;

    var sb = new StringBuilder();
    foreach(var part in input.Split('_'))
    {
        sb.Append(textInfo.ToTitleCase(part.ToLower()));
    }

    return sb.ToString();
}

Note that I am using the CultureInfo.CurrentCulture static property. I think it more accurately describes what you want...

Fiddle here

Comments

0

If you're after raw performance, you could operate directly on a char array:

private static string TitleCaseConvert(string title)
{
    int wordStart = 0;

    char[] result = new char[title.Length];
    int ri = 0;
    for (int i = 0; i < title.Length; ++i)
    {
        if (title[i] == '_')
        {
            wordStart = i + 1;
        }
        else if (i == wordStart)
        {
            result[ri++] = Char.ToUpper(title[i]);
        }
        else
        {
            result[ri++] = Char.ToLower(title[i]);
        }
    }

    return new String(result, 0, ri);
}

Otherwise, a StringBuilder implementation will probably be shorter and more readable.

Comments

0

You could toss Regex.Replace() at this thing to pick out the first character, dump the underscore, and capitalize it.

title = Regex.Replace("this_is_a_test", @"(^|_)([a-z])", m =>  m.ToString().Replace("_","").ToUpper());

1 Comment

This works if I do a ToLower function on the original string EMAIL_LOG. Thanks.
0

Alternative using Microsoft.VisualStudio :

var title = Strings.StrConv("EMAIL_LOG", VbStrConv.ProperCase).Replace("_", "");

Using Humanizer library :

var title = "EMAIL_LOG".ToLower().Pascalize();

Comments

-1

You have the _.camelCase function in any library as lodash.

_.camelCase("EMAIL_LOG") // "emailLog"

We add (also from lodash) the _.upperFirst function to put the first character in uppercase.

_.upperFirst( _.camelCase("EMAIL_LOG") ) // "EmailLog"

Camel Case

Camel case is the practice of writing compound words or phrases such that each word or abbreviation in the middle of the phrase begins with a capital letter, with no intervening spaces or punctuation.

Lodash

Lodash is a JavaScript library which provides utility functions for common programming tasks using the functional programming paradigm.

Comments

Your Answer

Post as a guest

Required, but never shown

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.

Morty Proxy This is a proxified and sanitized view of the page, visit original site.