Convert Dash-Separated String to camelCase via C#

前端 未结 5 1519
天涯浪人
天涯浪人 2021-01-21 20:56

I have a large XML file that contain tag names that implement the dash-separated naming convention. How can I use C# to convert the tag names to the camel case naming convention

相关标签:
5条回答
  • 2021-01-21 20:59

    Here is an updated version of @Jim Mischel's answer that will ignore the content - i.e. it will only camelCase tag names.

    string ConvertDashToCamelCase(string input)
    {
        StringBuilder sb = new StringBuilder();
        bool caseFlag = false;
        bool tagFlag = false; 
        for(int i = 0; i < input.Length; i++)
        {   
            char c = input[i];
            if(tagFlag)
            {
                if (c == '-')
                {
                    caseFlag = true;
                }
                else if (caseFlag)
                {
                    sb.Append(char.ToUpper(c));
                    caseFlag = false;
                }
                else
                {
                    sb.Append(char.ToLower(c));
                }
            }
            else
            {
                sb.Append(c);
            }
    
            // Reset tag flag if necessary
            if(c == '>' || c == '<')
            {
                tagFlag = (c == '<');
            }
    
        }
        return sb.ToString();
    }
    
    0 讨论(0)
  • 2021-01-21 21:16
    using System;
    using System.Text;
    
    public class MyString
    {
      public static string ToCamelCase(string str)
      {
        char[] s = str.ToCharArray();
        StringBuilder sb = new StringBuilder();
        for(int i = 0; i < s.Length; i++)
        {
          if (s[i] == '-' || s[i] == '_')
            sb.Append(Char.ToUpper(s[++i]));
          else
            sb.Append(s[i]);
        }
        return sb.ToString();
      }
    }
    
    0 讨论(0)
  • 2021-01-21 21:18

    The reason your original code was slow is because you're calling ToString all over the place unnecessarily. There's no need for that. There's also no need for the intermediate array of char. The following should be much faster, and faster than the version that uses String.Split, too.

    string ConvertDashToCamelCase(string input)
    {
        StringBuilder sb = new StringBuilder();
        bool caseFlag = false;
        for (int i = 0; i < input.Length; ++i)
        {
            char c = input[i];
            if (c == '-')
            {
                caseFlag = true;
            }
            else if (caseFlag)
            {
                sb.Append(char.ToUpper(c));
                caseFlag = false;
            }
            else
            {
                sb.Append(char.ToLower(c));
            }
        }
        return sb.ToString();
    }
    

    I'm not going to claim that the above is the fastest possible. In fact, there are several obvious optimizations that could save some time. But the above is clean and clear: easy to understand.

    The key is the caseFlag, which you use to indicate that the next character copied should be set to upper case. Also note that I don't automatically convert the entire string to lower case. There's no reason to, since you'll be looking at every character anyway and can do the appropriate conversion at that time.

    The idea here is that the code doesn't do any more work than it absolutely has to.

    0 讨论(0)
  • 2021-01-21 21:22

    For completeness, here's also a regular expression one-liner (inspred by this JavaScript answer):

    string ConvertDashToCamelCase(string input) =>
        Regex.Replace(input, "-.", m => m.Value.ToUpper().Substring(1));
    

    It replaces all occurrences of -x with x converted to upper case.


    Special cases:

    • If you want lower-case all other characters, replace input with input.ToLower() inside the expression:

        string ConvertDashToCamelCase(string input) =>
            Regex.Replace(input.ToLower(), "-.", m => m.Value.ToUpper().Substring(1));
      
    • If you want to support multiple dashes between words (dash--case) and have all of the dashes removed (dashCase), replace - with -+ in the regular expression (to greedily match all sequences of dashes) and keep only the final character:

        string ConvertDashToCamelCase(string input) =>
            Regex.Replace(input, "-+.", m => m.Value.ToUpper().Substring(m.Value.Length - 1));
      
    • If you want to support multiple dashes between words (dash--case) and remove only the final one (dash-Case), change the regular expression to match only a dash followed by a non-dash (rather than a dash followed by any character):

        string ConvertDashToCamelCase(string input) =>
            Regex.Replace(input, "-[^-]", m => m.Value.ToUpper().Substring(1));
      
    0 讨论(0)
  • 2021-01-21 21:25
    string ConvertDashToCamelCase(string input)
    {
        string[] words = input.Split('-');
    
        words = words.Select(element => wordToCamelCase(element));
    
        return string.Join("", words);
    }
    
    string wordToCamelCase(string input)
    {
        return input.First().ToString().ToUpper() + input.Substring(1).ToLower();
    }
    
    0 讨论(0)
提交回复
热议问题