簡體   English   中英

C# - 有一個索引數組,其中包含我想打印為字符串而不是字符的字符串

[英]C# - Have an index array full of strings that I want to print as strings instead of chars

當您的字符串在數組中被索引時,有人可以幫助我通過字符串插值打印出字符串而不是 char 嗎? 正如在if 語句中所看到的 - {text[3]}等。

static void Main(string[] args)
{

    string textDoc = "The first world war occurred in the early 20th century, many countries around the world were originally agnostic to the early battles. An obvious reason why is because there are many countries in the world that do not have relations to nation states that were fighting in the early battles.";

    string[] textSplit = textDoc.Split(" ");
    
    foreach (string text in textSplit) {
        if(text.StartsWith("a")) {
            Console.WriteLine($"Roses {text[2]} red, scarlets {text[2]} blue, this poem doesn't have {text[3]} ending, so embrace the unknown because it is {text[1]}.");
            break;
        }
    }

{text[3]} 打印出 char - "a" ,而不是字符串 - "are"

謝謝。

text是一個完整的單詞(作為字符串)

當您的foreach到達textSplit[12]時, text的值將是“周圍”

如果你然后使用text[3]你會在你的字符串的 position 3 中得到字符 'u'

如果您想要整個單詞“around”,只需使用text而不是text[3]

這個例子

暫無
暫無

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

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