Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ToPascalCase() C# for all caps Abbreviations

I am using ReSharper and trying to abide by it's default rules.

In part of my code, I need to change a string Property to PascalCase.

I have tried numerous methods but cannot find one that works for things which include all capital Abbreviations.

EX:

MPSUser --> Still MPSUser (should be MpsUser)
ArticleID --> Still Article ID (Should be ArticleId)
closeMethod --> Works and changes to CloseMethod

Can anyone help me create a method that can turn any String to PascalCase? Thanks!

like image 943
Torrey Braman Avatar asked Apr 28 '14 15:04

Torrey Braman


2 Answers

The only built-in method I know of for converting to PascalCase is TextInfo.ToTitleCase, and it doesn't handle all-caps words by design. To work around this, I have crafted a custom regular expression that can detect all the word parts, and then they are individually converted to Title/Pascal Case:

string ToPascalCase(string s)
{
    // Find word parts using the following rules:
    // 1. all lowercase starting at the beginning is a word
    // 2. all caps is a word.
    // 3. first letter caps, followed by all lowercase is a word
    // 4. the entire string must decompose into words according to 1,2,3.
    // Note that 2&3 together ensure MPSUser is parsed as "MPS" + "User".

    var m = Regex.Match(s, "^(?<word>^[a-z]+|[A-Z]+|[A-Z][a-z]+)+$");
    var g = m.Groups["word"];

    // Take each word and convert individually to TitleCase
    // to generate the final output.  Note the use of ToLower
    // before ToTitleCase because all caps is treated as an abbreviation.
    var t = Thread.CurrentThread.CurrentCulture.TextInfo;
    var sb = new StringBuilder();
    foreach (var c in g.Captures.Cast<Capture>())
        sb.Append(t.ToTitleCase(c.Value.ToLower()));
    return sb.ToString();
}

This function should handle the common use cases:

s           | ToPascalCase(s)
MPSUser     | MpsUser
ArticleID   | ArticleId
closeMethod | CloseMethod
like image 191
mellamokb Avatar answered Oct 04 '22 02:10

mellamokb


I borrowed heavily from mellamokb's solution to come up with something a little more comprehensive. For example, I wanted to leave numbers alone. Also, I wanted any non-letter, non-number character to be used as a delimiter. Here it is:

    public static string ToPascalCase(this string s) {
        var result = new StringBuilder();
        var nonWordChars = new Regex(@"[^a-zA-Z0-9]+");
        var tokens = nonWordChars.Split(s);
        foreach (var token in tokens) {
            result.Append(PascalCaseSingleWord(token));
        }

        return result.ToString();
    }

    static string PascalCaseSingleWord(string s) {
        var match = Regex.Match(s, @"^(?<word>\d+|^[a-z]+|[A-Z]+|[A-Z][a-z]+|\d[a-z]+)+$");
        var groups = match.Groups["word"];

        var textInfo = Thread.CurrentThread.CurrentCulture.TextInfo;
        var result = new StringBuilder();
        foreach (var capture in groups.Captures.Cast<Capture>()) {
            result.Append(textInfo.ToTitleCase(capture.Value.ToLower()));
        }
        return result.ToString();
    }

Here is an x-unit test that shows some test cases:

    [Theory]
    [InlineData("imAString", "ImAString")]
    [InlineData("imAlsoString", "ImAlsoString")]
    [InlineData("ImAlsoString", "ImAlsoString")]
    [InlineData("im_a_string", "ImAString")]
    [InlineData("im a string", "ImAString")]
    [InlineData("ABCAcronym", "AbcAcronym")]
    [InlineData("im_a_ABCAcronym", "ImAAbcAcronym")]
    [InlineData("im a ABCAcronym", "ImAAbcAcronym")]
    [InlineData("8ball", "8Ball")]
    [InlineData("im a 8ball", "ImA8Ball")]
    [InlineData("IM_ALL_CAPS", "ImAllCaps")]
    [InlineData("IM ALSO ALL CAPS", "ImAlsoAllCaps")]
    [InlineData("i-have-dashes", "IHaveDashes")]
    [InlineData("a8word_another_word", "A8WordAnotherWord")]
    public void WhenGivenString_ShouldPascalCaseIt(string input, string expectedResult) {
        var result = input.ToPascalCase();
        Assert.Equal(expectedResult, result);
    }
like image 45
kmkemp Avatar answered Oct 04 '22 02:10

kmkemp