Convert Dash-Separated String to camelCase via C#

前端 未结 5 1652
天涯浪人
天涯浪人 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 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();
      }
    }
    

提交回复
热议问题