[英]C# - Have an index array full of strings that I want to print as strings instead of chars
Can someone help me print out a string instead of a char via string interpolation when your strings are indexed in an array?当您的字符串在数组中被索引时,有人可以帮助我通过字符串插值打印出字符串而不是 char 吗? As seen the printed in the if statement - {text[3]} etc.
正如在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]} prints out the char - "a" , instead of a string - "are" . {text[3]} 打印出 char - "a" ,而不是字符串 - "are" 。
Thanks.谢谢。
text
is an entire word (as a string) text
是一个完整的单词(作为字符串)
When your foreach
gets to textSplit[12]
the value of text
will be "around"当您的
foreach
到达textSplit[12]
时, text
的值将是“周围”
If you then use text[3]
you'll get the char in position 3 of your string which is 'u'如果你然后使用
text[3]
你会在你的字符串的 position 3 中得到字符 'u'
If you want the entire word "around" just use text
instead of text[3]
如果您想要整个单词“around”,只需使用
text
而不是text[3]
See this example看这个例子
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.