概述
- public string ApplyPascalCase(string name)
- {
- string notStartingAlpha = Regex.Replace(name, "^[^a-zA-Z]+", "");
- string workingString = ToLowerExceptCamelCase(notStartingAlpha);
- workingString = RemoveSeparatorAndCapNext(workingString);
- return workingString;
- }
- public string ToLowerExceptCamelCase(string input)
- {
- char[] chars = input.ToCharArray();
- for (int i = 0; i < chars.Length; i++)
- {
- int left = (i > 0 ? i - 1 : i);
- int right = (i < chars.Length - 1 ? i + 1 : i);
- if (i != left && i != right)
- {
- if (Char.IsUpper(chars[i]) && Char.IsLetter(chars[left]) && Char.IsUpper(chars[left]))
- {
- chars[i] = Char.ToLower(chars[i], CultureInfo.InvariantCulture);
- }
- else if (Char.IsUpper(chars[i]) && Char.IsLetter(chars[right]) && Char.IsUpper(chars[right]))
- {
- chars[i] = Char.ToLower(chars[i], CultureInfo.InvariantCulture);
- }
- else if (Char.IsUpper(chars[i]) && !Char.IsLetter(chars[right]))
- {
- chars[i] = Char.ToLower(chars[i], CultureInfo.InvariantCulture);
- }
- }
- }
- if (chars.Length > 0)
- chars[chars.Length - 1] = Char.ToLower(chars[chars.Length - 1], CultureInfo.InvariantCulture);
- return new string(chars);
- }
- public string RemoveSeparatorAndCapNext(string input)
- {
- string dashUnderscore = "-_";
- string workingString = input;
- char[] chars = workingString.ToCharArray();
- int under = workingString.IndexOfAny(dashUnderscore.ToCharArray());
- while (under > -1)
- {
- chars[under + 1] = Char.ToUpper(chars[under + 1], CultureInfo.InvariantCulture);
- workingString = new String(chars);
- under = workingString.IndexOfAny(dashUnderscore.ToCharArray(), under + 1);
- }
- chars[0] = Char.ToUpper(chars[0], CultureInfo.InvariantCulture);
- workingString = new string(chars);
- return Regex.Replace(workingString, "[-_]", "");
- 注:输入字符串必须以: CODE-NAME 或者 CODE_NAME方式才可正确识别
最后
以上就是悲凉水杯为你收集整理的实现字符串以PascalCase方式命名的全部内容,希望文章能够帮你解决实现字符串以PascalCase方式命名所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复