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

问题描述 投票:-1回答:4

我正在学习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?";

该怎么办?

c# string toupper
4个回答
0
投票

您可以使用Substring选择要转换为大写字母的字符,然后使用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(" ");
}

1
投票

不需要第一个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类用于此类字符串操作。要使用它,只需在文件顶部添加using System.Text


0
投票

这是我的看法:

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());

[您有充分的机会来建立一个新的印象/顾问并根据我们的建议和判断/您可以确定您是否想////////// era / never od或even / madam,我是adam /您是否打算这么做?是不是由于MorRow,是不是?


0
投票

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

实现要求的一种简单方法是使用ToCharArray将字符串转换为char数组,然后使用简单的for循环进行迭代。在循环内,您可以检查该位置的char是否既是字母又是大于或等于ASCII97。小写字母从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);
© www.soinside.com 2019 - 2024. All rights reserved.