Split PascalCase string except for acronyms

。_饼干妹妹 提交于 2019-12-05 05:39:05

Wouldn't [A-Z]+[a-z]* do it? That should match one or more upper-case letters followed by zero or more lower-case letters. So ARBs would remain a single entity, but CamelCase would be split into Camel Case.

kgoedtel

How about this?

[A-Z][a-z]+|[A-Z]

A string/paragraph/sentence including Acronyms can be converted to Human readable sentences/string. I was just trying for formatting of Pascal Cased string, i investigated more and tried even to convert Acronyms in to Understandable format.

Test Data:

Input: "QWERTYSomeThing OmitTRYSomeThing MayBeWorkingFYI"

Output: "QWERTY Some Thing Omit TRY Some Thing May Be Working FYI"

Code: Pass Input String to Method Given Below.

    private static string FormatPascalAndAcronym(string input)
    {
        var builder = new StringBuilder(input[0].ToString());
        if (builder.Length > 0)
        {
            for (var index = 1; index < input.Length; index++)
            {
                char prevChar = input[index - 1];
                char nextChar = index + 1 < input.Length ? input[index + 1] : '\0';

                bool isNextLower = Char.IsLower(nextChar);
                bool isNextUpper = Char.IsUpper(nextChar);
                bool isPresentUpper = Char.IsUpper(input[index]);
                bool isPrevLower = Char.IsLower(prevChar);
                bool isPrevUpper = Char.IsUpper(prevChar);

                if(!string.IsNullOrWhiteSpace(prevChar.ToString()) && 
                    ((isPrevUpper&& isPresentUpper && isNextLower) || 
                    (isPrevLower&&isPresentUpper&&isNextLower)||
                    (isPrevLower&&isPresentUpper&&isNextUpper)))
                {
                    builder.Append(' ');
                    builder.Append(input[index]);
                }
                else{
                builder.Append(input[index]);
                }
            }
        }
        return builder.ToString();
    }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!