Splitting CamelCase

前端 未结 15 2469
盖世英雄少女心
盖世英雄少女心 2020-12-07 10:56

This is all asp.net c#.

I have an enum

public enum ControlSelectionType 
{
    NotApplicable = 1,
    SingleSelectRadioButtons = 2,
    SingleSelectD         


        
15条回答
  •  爱一瞬间的悲伤
    2020-12-07 11:34

    #JustSayNoToRegex

    Takes a C# identifier, with uderscores and numbers, and converts it to space-separated string.

    public static class StringExtensions
    {
        public static string SplitOnCase(this string identifier)
        {
            if (identifier == null || identifier.Length == 0) return string.Empty;
            var sb = new StringBuilder();
    
            if (identifier.Length == 1) sb.Append(char.ToUpperInvariant(identifier[0]));
    
            else if (identifier.Length == 2) sb.Append(char.ToUpperInvariant(identifier[0])).Append(identifier[1]);
    
            else {
                if (identifier[0] != '_') sb.Append(char.ToUpperInvariant(identifier[0]));
                for (int i = 1; i < identifier.Length; i++) {
                    var current = identifier[i];
                    var previous = identifier[i - 1];
    
                    if (current == '_' && previous == '_') continue;
    
                    else if (current == '_') {
                        sb.Append(' ');
                    }
    
                    else if (char.IsLetter(current) && previous == '_') {
                        sb.Append(char.ToUpperInvariant(current));
                    }
    
                    else if (char.IsDigit(current) && char.IsLetter(previous)) {
                        sb.Append(' ').Append(current);
                    }
    
                    else if (char.IsLetter(current) && char.IsDigit(previous)) {
                        sb.Append(' ').Append(char.ToUpperInvariant(current));
                    }
    
                    else if (char.IsUpper(current) && char.IsLower(previous) 
                        && (i < identifier.Length - 1 && char.IsUpper(identifier[i + 1]) || i == identifier.Length - 1)) {
                            sb.Append(' ').Append(current);
                    }
    
                    else if (char.IsUpper(current) && i < identifier.Length - 1 && char.IsLower(identifier[i + 1])) {
                        sb.Append(' ').Append(current);
                    }
    
                    else {
                        sb.Append(current);
                    }
                }
            }
            return sb.ToString();
        }
    
    }
    

    Tests:

    [TestFixture]
    static class HelpersTests
    {
        [Test]
        public static void Basic()
        {
            Assert.AreEqual("Foo", "foo".SplitOnCase());
            Assert.AreEqual("Foo", "_foo".SplitOnCase());
            Assert.AreEqual("Foo", "__foo".SplitOnCase());
            Assert.AreEqual("Foo", "___foo".SplitOnCase());
            Assert.AreEqual("Foo 2", "foo2".SplitOnCase());
            Assert.AreEqual("Foo 23", "foo23".SplitOnCase());
            Assert.AreEqual("Foo 23 A", "foo23A".SplitOnCase());
            Assert.AreEqual("Foo 23 Ab", "foo23Ab".SplitOnCase());
            Assert.AreEqual("Foo 23 Ab", "foo23_ab".SplitOnCase());
            Assert.AreEqual("Foo 23 Ab", "foo23___ab".SplitOnCase());
            Assert.AreEqual("Foo 23", "foo__23".SplitOnCase());
            Assert.AreEqual("Foo Bar", "Foo_bar".SplitOnCase());
            Assert.AreEqual("Foo Bar", "Foo____bar".SplitOnCase());
            Assert.AreEqual("AAA", "AAA".SplitOnCase());
            Assert.AreEqual("Foo A Aa", "fooAAa".SplitOnCase());
            Assert.AreEqual("Foo AAA", "fooAAA".SplitOnCase());
            Assert.AreEqual("Foo Bar", "FooBar".SplitOnCase());
            Assert.AreEqual("Mn M", "MnM".SplitOnCase());
            Assert.AreEqual("AS", "aS".SplitOnCase());
            Assert.AreEqual("As", "as".SplitOnCase());
            Assert.AreEqual("A", "a".SplitOnCase());
            Assert.AreEqual("_", "_".SplitOnCase());
    
        }
    }
    

提交回复
热议问题