代码之家  ›  专栏  ›  技术社区  ›  user8512043

将任何Sting转换为Camel案例

c#
  •  -4
  • user8512043  · 技术社区  · 1 年前

    我正在尝试将一种类型的字符串转换为驼色大小写。这是字符串:

    here %it -RAINS
    

    预期输出 :

    hereItRains
    

    我尝试了以下操作:

    public static string ToCamelCase(this string str)
    {
        var words = str.Split(new[] { "_", "-", "%" }, StringSplitOptions.RemoveEmptyEntries);
    
        words = words.Select(word => char.ToUpper(word[0]) + word.Substring(1)).ToArray();
        
        return string.Join(string.Empty, words);
    }
    

    得到的输出如下:

    Here It RAINS
    

    这里有什么我错过的吗?这是我迄今为止尝试的链接- CamelCase

    2 回复  |  直到 1 年前
        1
  •  3
  •   Pedro Paulo    1 年前

    这里已经有很多答案了,但我想添加一个新的答案,即使用原始C#代码来处理字符串,避免一些内存分配并减少处理时间。

    public static string ToCamelCase(this string value)
    {
        if (string.IsNullOrWhiteSpace(value))
            return value;
    
        var upperCase = false;
        var sb = new StringBuilder();
    
        for (int i = 0; i < value.Length; i++)
        {
            if (char.IsWhiteSpace(value[i]) || char.IsPunctuation(value[i]))
            {
                upperCase = true;
                continue;
            }
    
            char ch;
    
            if (upperCase)
            {
                ch = char.ToUpperInvariant(value[i]);
            }
            else
            {
                ch = char.IsLower(value[i]) ? value[i] : char.ToLowerInvariant(value[i]);
            }
    
            upperCase = false;
    
            sb.Append(ch);
        }
    
        return sb.ToString();
    }
    

    当我们使用ToArray、Split、ToLower、Regex等方法时,我们会在内存中创建许多对象,这可以通过一些逻辑来避免。

    基准测试

    Benchmark tests

        2
  •  2
  •   Dmitry Bychenko    1 年前

    我建议使用 正则表达式 为了匹配我们应该变成小写(第一个单词)或标题大小写(所有其他单词)的所有单词:

    using System.Globalization;
    using System.Linq;
    using System.Text.RegularExpressions;
    
    ...
    
    public static string ToCamelCase(string value) {
      if (string.IsNullOrEmpty(value))
        return value;
    
      return string.Concat(Regex
        .Matches(value, @"\p{L}+")
        .Select(match => match.Value.ToLower(CultureInfo.InvariantCulture))
        .Select((word, index) => index == 0
           ? word
           : CultureInfo.InvariantCulture.TextInfo.ToTitleCase(word)));
    }
    

    Fiddle

    评论:

    1. \p{L}+ pattern代表“一个或多个” Unicode 字母”。
    2. ToTitleCase 将除所有大写字母外的任何单词都改为大小写(这些单词被视为首字母缩写,不会更改),这就是我称之为 ToLower() 任何单词。
        3
  •  1
  •   Hossein Sabziani    1 年前

    编辑 ToCamelCase 功能如下:

    public string ToCamelCase(string str)
    {
        var allWords = str.Split(new[] { "_", "-", "%", " " }, StringSplitOptions.RemoveEmptyEntries).ToList();                      
        var words = allWords.Skip(1).Select(word => char.ToUpper(word[0]) + word.Substring(1).ToLower()).ToList();
        words.Insert(0, allWords[0]);
        return string.Join(string.Empty, words);
    }
    
    1. 添加 " " 添加到您的Split系列中。

    2. 添加 .ToLower() 对单词。衬底(1)

    3. 跳过第一个单词with allWords.Skip(1)

    后果

    a b c %d ==> aBCD
    here %it -RAINS ==> hereItRains
    
        4
  •  1
  •   Jeremy Caney NotSoImportant    1 年前

    我为我的一项大学作业做了以下代码,效果非常好。这个 ToCamelCase 函数接受输入文本,使用空格、下划线和连字符作为分隔符将其分解为单词,然后将每个单词转换为除第一个单词外的标题大小写(大写首字母)。这个 TextInfo 类根据当前的区域性处理正确的案例。

    class Program
    {
        static string ToCamelCase(string input)
        {
            TextInfo textInfo = new CultureInfo("en-US", false).TextInfo;
            string[] words = input.Split(new[] { ' ', '_', '-' }, StringSplitOptions.RemoveEmptyEntries);
    
            if (words.Length == 0)
                return input;
    
            string camelCase = words[0].ToLower();
            for (int i = 1; i < words.Length; i++)
            {
                camelCase += textInfo.ToTitleCase(words[i].ToLower());
            }
    
            return camelCase;
        }
    
        static void Main(string[] args)
        {
            string input = "convert_this_string_to_camel_case";
            string camelCaseOutput = ToCamelCase(input);
    
            Console.WriteLine(camelCaseOutput);
        }
    }