Convert String To camelCase from TitleCase C#

后端 未结 11 1652

I have a string that I converted to a TextInfo.ToTitleCase and removed the underscores and joined the string together. Now I need to change the first and only the first charact

11条回答
  •  半阙折子戏
    2021-02-04 23:58

    Adapted from Leonardo's answer:

    static string PascalCase(string str) {
      TextInfo cultInfo = new CultureInfo("en-US", false).TextInfo;
      str = Regex.Replace(str, "([A-Z]+)", " $1");
      str = cultInfo.ToTitleCase(str);
      str = str.Replace(" ", "");
      return str;
    }
    

    Converts to PascalCase by first adding a space before any group of capitals, and then converting to title case before removing all the spaces.

提交回复
热议问题