如何在我的代码 C# 中将每三个字符大写?

How do I capitalize every third character in my code C#?

我正在学习 C#,但有些事情让我很沮丧。我正在学习字符串方法及其工作原理。

public static void CaseFlip()
        {
            Console.WriteLine("             CaseFlip -- Output");
            Console.WriteLine("==============================================================================");


           for (int i = 0; i < text.Length; i ++)
           {
            char[]delimiters = {'/'};
            string[]splitString = text.Split(delimiters);


                for (int k  = 0; k < splitString.Length; k +=3)
                    {
                    string sub = text.Substring(0);
                    string remove = sub.Remove(4, text.Length);
                    string insert = remove.Insert(0, sub);

                    splitString[k] = splitString[k].ToUpper();
                    Console.WriteLine(splitString[k]);
                    }
           }
           Console.WriteLine(" ");

        }

我的字符串数组是:

static string text = "You only have one chance to make a first impression/" +
        "Consider the source and judge accordingly/" +
        "You can do something for a day you can't imagine doing for a lifetime/" +
        "Are we not drawn onward, we few, drawn onward to new era/" +
        "Never odd or even/" +
        "Madam, I'm Adam/" +
        "What do you mean? It's not due tomorrow, is it?";

怎么办?

第一个for循环不需要;单个循环应该迭代与文本中一样多的分隔符。 你也会在这一行中得到一个例外

string remove = sub.Remove(4, text.Length);

因为您试图通过删除整个文本的一部分来创建一个新字符串,从第 4 个字符开始,并尽可能多的字符 text.Length - 实际上超出了范围。 试试这个:

public static void CaseFlip(string text)
{
    Console.WriteLine("             CaseFlip -- Output");
    Console.WriteLine("==============================================================================");

    char[] delimiters = { '/' };
    string[] splitString = text.Split(delimiters);
    StringBuilder sb = new StringBuilder();

    for (int i = 0; i < splitString.Length; i++)
    {
        char[] charsInLine = splitString[i].ToCharArray();

        for (int k = 0; k < charsInLine.Length; k++)
        {
            sb.Append(k % 3 == 0 ? char.ToUpper(charsInLine[k]) : charsInLine[k]);
        }
        sb.Append(' ');
    }

    Console.WriteLine(sb.ToString());
    Console.WriteLine(" ");
}

您应该考虑使用 StringBuilder class 进行这样的字符串操作。要使用它,只需将 using System.Text 添加到文件顶部即可。

你可以用Substring把你要转换成大写的字符用select,然后用Remove把原来的字符从字符串中去掉,Insert 以重新添加大写字符。由于字符串是不可变的,因此您还需要将中间步骤存储在变量中,以便将更改延续到下一个循环。

public static void CaseFlip()
{
    Console.WriteLine("             CaseFlip -- Output");
    Console.WriteLine("==============================================================================");

    var splitString = text.Split('/');
    for (var i = 0; i < splitString.Length; i++)
    {
        var line = splitString[i];
        for (var k = 0; k < line.Length; k += 3)
        {
            var upperCasedCharacter = line.Substring(k, 1).ToUpper();
            line = line.Remove(k, 1).Insert(k, upperCasedCharacter);
        }

        Console.WriteLine(line);
    }

    Console.WriteLine(" ");
}

字符串是不可变的。调用 SubStringRemoveInsert 每次都会创建一个新字符串,并且不会占用内存。

实现您所要求的一种简单方法是使用 ToCharArray 将字符串转换为 char 数组,然后使用简单的 for 循环进行迭代。在循环内部,您可以检查该位置的字符是否既是字母又大于或等于 ASCII 97。小写字母从 97 开始。

将小写字母转换为大写字母就是将字母减去32。由于您修改的是 char 数组,因此不会在内存中创建任何新字符串。

大写完成后,您可以使用其中一个 String 构造函数再次将 char 数组转换为字符串。

var chars = text.ToCharArray();
for (int i = 0; i < chars.Length; i += 3)
{
    if (char.IsLetter(chars[i]) && chars[i] >= 'a')
        chars[i] = (char)(chars[i] - 32);
}

text = new string(chars);

这是我对此的看法:

string text =
    "You only have one chance to make a first impression/" +
    "Consider the source and judge accordingly/" +
    "You can do something for a day you can't imagine doing for a lifetime/" +
    "Are we not drawn onward, we few, drawn onward to new era/" +
    "Never odd or even/" +
    "Madam, I'm Adam/" +
    "What do you mean? It's not due tomorrow, is it?";

string result = new string(
    text
        .Select((c, i) => i % 3 == 0 ? char.ToUpper(c) : c)
        .ToArray());

You onLy HavE oNe ChaNce to maKe A fIrsT iMprEssIon/CoNsiDer thE sOurCe And juDge acCorDinGly/YoU cAn Do SomEthIng foR a daY yOu Can't ImaGinE dOinG fOr A lIfeTimE/ARe We Not drAwn onWarD, We Few, dRawN oNwaRd To New erA/NEveR oDd Or EveN/MAdaM, I'm AdAm/WhaT dO yOu MeaN? It'S nOt Due toMorRow, iS iT?