C# 如何使用字符串拆分计算行数

C# 如何使用字符串拆分计算行数,c#,C#,我正试图完成一项几周前到期的练习作业(未评分),但我一辈子都搞不清楚到底出了什么问题。我需要输出为 “1 2快速 3棕色 4狐狸 5跳 六点多 7 8懒惰的 9“狗” 我有拆分字符串,所以我可以把所有的单词都记下来,只是不能得到前面的数字。到目前为止,我有这个 int ctr = 0; string sentance = "The quick brown fox jumped over the lazy dog. "; string[] split

我正试图完成一项几周前到期的练习作业(未评分),但我一辈子都搞不清楚到底出了什么问题。我需要输出为 “1 2快速 3棕色 4狐狸 5跳 六点多 7 8懒惰的 9“狗” 我有拆分字符串,所以我可以把所有的单词都记下来,只是不能得到前面的数字。到目前为止,我有这个

int ctr = 0;
            string sentance = "The quick brown fox jumped over the lazy dog. ";
            string[] split = sentance.Split(new char[] { ' ', '.' });
            foreach (string s in split)
            {
                if (s.Trim() != "")
                    Console.WriteLine("{0} {2}", ctr++, s);
            }
            Console.ReadLine();

我一直得到的只是一个未处理的异常,我无法找出原因。

我将字符串数组的每个元素附加到字符串“result”中,然后将整个内容打印到for循环后

我试着用一种简单的方法来实现连接,但是你可以使用一些其他的函数来让它更漂亮,而且不需要手动操作,希望能有所帮助

        string sentence = "The quick brown fox jumped over the lazy dog. ";
        string[] split = sentence.Split(new char[] { ' ', '.' });
        string result = "";
        int i = 1;
        foreach (string s in split)
        {
            if (s.Trim() != ""){
                result += (i + " " + s + " ");
                i++;
            }
        }
        Console.WriteLine(result);
排队

Console.WriteLine("{0} {2}", ctr++, s);
{0}
引用下面列表中的第0个元素;这将是
ctr++
{1}
,如果您使用它,将引用下一个
s
{2}
,您确实使用过它,如果有下一个,它将引用下一个,但没有下一个;这就是你的问题

{2}
没有#2变量(计数为0、1、2)可填充

试一试

您应该看到
{1}
拾取
s

您的错误消息可能类似于“索引(基于零)必须大于或等于零且小于参数列表的大小”

这意味着
{2}
中的索引2就是问题所在。它满足第一部分(“大于或等于零”),但不满足第二部分(“小于参数列表的大小”),因为您的参数列表只有2长(
ctr++
s
),但2(您在
{2}
中的索引)不小于2(参数列表的大小)


您的
ctr
的值也有一个“一关一关”的问题,但这应该足以让您回到正轨。

您的代码似乎很好。您只需要使用
“{0}{1}”
而不是
“{0}{2}”
,因为您需要索引0和1。另一个有趣的选项是使用
Linq
库。要确保输出为“1…2…3…”而不是“0…1…2…”,您需要确保使用
++ctr
而不是
ctr++
。这确保在增量之后而不是之前返回值

使用
System.Linq
库,您可以简单地使用以下内容:

string sentance = "The quick brown fox jumped over the lazy dog. ";
string[] split = sentance.Split(new char[] { ' ', '.' });
string result = string.Join(" ", from w in split 
                                    where w.Trim() != ""
                                    select ++ctr + " " + w);

您的代码应该如下所示:

int ctr = 0;
string sentance = "The quick brown fox jumped over the lazy dog. ";
string[] split = sentance.Split(new char[] { ' ', '.' });

foreach (string s in split)
{
    if (!string.IsNullOrWhiteSpace(s))
        Console.WriteLine("{0} {1}", ctr++, s);
}

Console.ReadLine();
更多关于这里


此外,String.Split还带有重载。其中之一是
删除mptyentries
。如果需要删除空条目并避免检查Trim()等,则可以使用它。更多信息。

输出应该是以每个数字开头的新行,以打印问题中的整个详细信息(
System.FormatException
)。刚才提到的未处理,有一个例外提示。还有一个函数
string.IsNullOrWhiteSpace
用于打印输出,它应该是{1}而不是{2}。或者您可以使用诸如Console.WriteLine($“{ctr++}{s}”)之类的字符串插值格式。注意此功能可从C#6获得。谢谢!我不敢相信它这么简单,花了4个小时我想我看多了!我很快就会修好的!很乐意帮忙。如果你觉得它有帮助,请接受它作为一个答案。谢谢!我不敢相信它这么简单,花了4个小时我想我看多了!我很快就会修好的!一定要看看林克。它使事情变得非常简单。
int ctr = 0;
string sentance = "The quick brown fox jumped over the lazy dog. ";
string[] split = sentance.Split(new char[] { ' ', '.' });

foreach (string s in split)
{
    if (!string.IsNullOrWhiteSpace(s))
        Console.WriteLine("{0} {1}", ctr++, s);
}

Console.ReadLine();