【问题标题】:C# - Have an index array full of strings that I want to print as strings instead of charsC# - 有一个索引数组,其中包含我想打印为字符串而不是字符的字符串
【发布时间】:2022-01-15 15:18:58
【问题描述】:

当您的字符串在数组中编入索引时,有人可以帮我通过字符串插值打印出一个字符串而不是一个字符吗?打印在 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]} 打印出字符 - "a",而不是字符串 - "are"

谢谢。

【问题讨论】:

    标签: c# arrays string char string-interpolation


    【解决方案1】:
    using System;
    using System.Linq;
    
    public class Program
    {
        public static void Main()
        {
            //options/configuration values which may work better as parameters
            string source = "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 template = "Roses {0} red, scarlets {1} blue, this poem doesn't have {2} ending, so embrace the unknown because it is {3}.";
            //When true both "around" and "An" will match "a". When false only "around" will
            bool ignoreCase = true;
            int numberOfBlanks = 4;
            //word to use in blanks if not enough matching words are found. use an empty string "" if you don't want anything filled
            string defaultWord = "banana";
            string matchingLetter = "a";
            
            string[] madgabWords = new string[numberOfBlanks];
            Array.Fill(madgabWords, defaultWord);
            
            
            string[] textSplit = source.Split(" ")
              .Where(x=> x.StartsWith(matchingLetter, ignoreCase, null))
              .ToArray();
            
            for(int i = 0; i < textSplit.Length && i < numberOfBlanks; i++)
            {
                madgabWords[i] = textSplit[i];
            }
            
            Console.WriteLine(template, madgabWords);
            //if you are building a string to return or assign to a variable instead of printing directly to console then use:
            var madgab = string.Format(template, madgabWords);
        }
    }
    

    你也可以试试online

    【讨论】:

    • 谢谢@DCAggie,这有点帮助,但如果我只在每个插值上使用text,我只会在我想要以'a'开头的不同单词变体时打印“around”这个词可见于string textDoc。那么我将如何打印以“a”开头的第二个text,这是“不可知的”。
    • 啊,那我误解了你要做什么。公平地说,您的目标是:从提供的字符串中提取以给定字母开头的所有单词,然后使用这些单词填充 madgab 中的空白”?
    • 是的,没错!
    • 我已经更新了我的答案,试一试
    • @StuCM 好运吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-22
    • 1970-01-01
    • 2013-11-26
    • 1970-01-01
    • 1970-01-01
    • 2021-08-06
    相关资源
    最近更新 更多