簡體   English   中英

如何在我的代碼 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類用於這樣的字符串操作。 要使用它,只需將using System.Text添加到文件頂部。

您可以使用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(" ");
}

字符串是不可變的。 調用SubStringRemoveInsert每次都會創建一個新字符串並且不使用內存。

實現您所要求的一種簡單方法是使用ToCharArray將字符串轉換為字符數組,然后使用簡單的 for 循環進行迭代。 在循環內部,您可以檢查該位置的字符是否既是字母又大於或等於ASCII 97。小寫字母從 97 開始。

要將小寫字母轉換為大寫字母,只需從字母中減去 32。 由於您正在修改字符數組,因此您不會在內存中創建任何新字符串。

大寫完成后,您可以使用 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());

你只有第一次印象/考慮來源並做出相應判斷/你可以做一些你無法想象的事情從不奇怪或偶數/女士,我是亞當/你是什么意思? 不是因為明天,是嗎?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM