83

TextInfo.ToTitleCase に変換し、アンダースコアを削除して文字列を結合した文字列があります。今、文字列の最初と最初の文字だけを小文字に変更する必要がありますが、何らかの理由でそれを達成する方法がわかりません。助けてくれてありがとう。

class Program
{
    static void Main(string[] args)
    {
        string functionName = "zebulans_nightmare";
        TextInfo txtInfo = new CultureInfo("en-us", false).TextInfo;
        functionName = txtInfo.ToTitleCase(functionName).Replace('_', ' ').Replace(" ", String.Empty);
        Console.Out.WriteLine(functionName);
        Console.ReadLine();
    }
}

結果: ZebulansNightmare

望ましい結果: zebulansNightmare

アップデート:

class Program
{
    static void Main(string[] args)
    {
        string functionName = "zebulans_nightmare";
        TextInfo txtInfo = new CultureInfo("en-us", false).TextInfo;
        functionName = txtInfo.ToTitleCase(functionName).Replace("_", string.Empty).Replace(" ", string.Empty);
        functionName = $"{functionName.First().ToString().ToLowerInvariant()}{functionName.Substring(1)}";
        Console.Out.WriteLine(functionName);
        Console.ReadLine();
    }
}

目的の出力を生成します

4

14 に答える 14

2

ここに私のコードがあり、すべての上位プレフィックスを下げることが含まれています:

public static class StringExtensions
{
    public static string ToCamelCase(this string str)
    {
        bool hasValue = !string.IsNullOrEmpty(str);

        // doesn't have a value or already a camelCased word
        if (!hasValue || (hasValue && Char.IsLower(str[0])))
        {
            return str;
        }

        string finalStr = "";

        int len = str.Length;
        int idx = 0;

        char nextChar = str[idx];

        while (Char.IsUpper(nextChar))
        {
            finalStr += char.ToLowerInvariant(nextChar);

            if (len - 1 == idx)
            {
                // end of string
                break;
            }

            nextChar = str[++idx];
        }

        // if not end of string 
        if (idx != len - 1)
        {
            finalStr += str.Substring(idx);
        }

        return finalStr;
    }
}

次のように使用します。

string camelCasedDob = "DOB".ToCamelCase();
于 2019-12-23T11:42:00.680 に答える
1

文字列は不変ですが、安全でないコードを使用して変更可能にすることはできます。string.Copy は、元の文字列がそのまま残ることを保証します。

これらのコードを実行するには、プロジェクトで安全でないコードを許可する必要があります。

        public static unsafe string ToCamelCase(this string value)
        {
            if (value == null || value.Length == 0)
            {
                return value;
            }

            string result = string.Copy(value);

            fixed (char* chr = result)
            {
                char valueChar = *chr;
                *chr = char.ToLowerInvariant(valueChar);
            }

            return result;
        }

このバージョンは、変更されたコピーを返すのではなく、元の文字列を変更します。これは面倒ですが、まったく珍しいことです。そのため、XML コメントがユーザーにそのことを警告していることを確認してください。

        public static unsafe void ToCamelCase(this string value)
        {
            if (value == null || value.Length == 0)
            {
                return value;
            }

            fixed (char* chr = value)
            {
                char valueChar = *chr;
                *chr = char.ToLowerInvariant(valueChar);
            }

            return value;
        }

しかし、なぜアンセーフ コードを使用するのでしょうか。簡単な答え...超高速です。

于 2020-06-18T20:01:37.027 に答える