is there a elegant way to parse a word and add spaces before capital letters

后端 未结 7 722
遥遥无期
遥遥无期 2020-12-01 09:43

i need to parse some data and i want to convert

AutomaticTrackingSystem

to

Automatic Tracking System

esse

相关标签:
7条回答
  • 2020-12-01 10:07

    Without regex you can do something like (or perhaps something more concise using LINQ):

    (Note: no error checking is there, you should add it)

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace SO
    {
        class Program
        {
            static void Main(string[] args)
            {
                String test = "AStringInCamelCase";
                StringBuilder sb = new StringBuilder();
    
                foreach (char c in test)
                {
                    if (Char.IsUpper(c))
                    {
                        sb.Append(" ");
                    }
                    sb.Append(c);
                }
    
                if (test != null && test.Length > 0 && Char.IsUpper(test[0]))
                {
                    sb.Remove(0, 1);
                }
    
                String result = sb.ToString();
                Console.WriteLine(result);
            }
        }
    }
    

    this gives the output

    A String In Camel Case
    
    0 讨论(0)
提交回复
热议问题