Splitting CamelCase

前端 未结 15 2468
盖世英雄少女心
盖世英雄少女心 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:39

    Try this:

    using System;
    using System.Linq;
    using System.Collections.Generic;
    
    public class Program
    {
        public static void Main()
        {
            Console
                .WriteLine(
                    SeparateByCamelCase("TestString") == "Test String" // True
                );
        }
    
        public static string SeparateByCamelCase(string str)
        {
            return String.Join(" ", SplitByCamelCase(str));
        }
    
        public static IEnumerable SplitByCamelCase(string str) 
        {
            if (str.Length == 0) 
                return new List();
    
            return 
                new List 
                { 
                    Head(str) 
                }
                .Concat(
                    SplitByCamelCase(
                        Tail(str)
                    )
                );
        }
    
        public static string Head(string str)
        {
            return new String(
                        str
                            .Take(1)
                            .Concat(
                                str
                                    .Skip(1)
                                    .TakeWhile(IsLower)
                            )
                            .ToArray()
                    );
        }
    
        public static string Tail(string str)
        {
            return new String(
                        str
                            .Skip(
                                Head(str).Length
                            )
                            .ToArray()
                    );
        }
    
        public static bool IsLower(char ch) 
        {
            return ch >= 'a' && ch <= 'z';
        }
    }
    

    See sample online

提交回复
热议问题