C# For循环找不到字符串数组中的所有单词

C# For循环找不到字符串数组中的所有单词,c#,arrays,C#,Arrays,我正在尝试将国家添加到Excel文档中,其中包含一些关于收养的信息。该信息在线发布于。我一直在做这件事,所以我至少可以在接下来的几个小时里展示一些东西。我已经在这个问题的底部添加了我真正想做的事情,以防你好奇 我发现问题出在回车和换行上。我正在努力解决这个问题,然后上传解决方案 我正在传递一个包含大约450个单词的字符串。我有两个'>>'和两个'>'。有什么我遗漏的吗?我要开始拔头发了 private void FindCountries(string text) { text

我正在尝试将国家添加到Excel文档中,其中包含一些关于收养的信息。该信息在线发布于。我一直在做这件事,所以我至少可以在接下来的几个小时里展示一些东西。我已经在这个问题的底部添加了我真正想做的事情,以防你好奇

我发现问题出在回车和换行上。我正在努力解决这个问题,然后上传解决方案

我正在传递一个包含大约450个单词的字符串。我有两个'>>'和两个'>'。有什么我遗漏的吗?我要开始拔头发了

private void FindCountries(string text) {
        text = text.Replace("\n", " "); //This 3 lines fixed the problem. 
        text = text.Replace("\r", " "); //Without these and string in the array was 
        text = text.Replace("\t", " "); //looking like this: "<<\r\n\r>>>" 
        string[] helper = text.Split(' ');
        //List<string> myContries = new List<string>();
        List<int> j = new List<int>();
        List<int> k = new List<int>();

        for (int i = 0; i < helper.Length; i++) {
            if (helper[i] == ">>>") {
            Console.WriteLine(helper[i]);
                Console.WriteLine("added to j");
                j.Add(i);
            }
            if (helper[i] == "<<") {
                Console.WriteLine("added to k");
                k.Add(i);
            }
        }
        Console.WriteLine("J");
        foreach (var item in j) {
            Console.WriteLine(item);
        }
        Console.WriteLine("K");
        foreach (var item in k) {
            Console.WriteLine(item);
        }
    }
private void FindCountries(字符串文本){
text=text.Replace(“\n”和“”);//这三行解决了这个问题。
text=text.Replace(“\r”,”);//如果没有这些,则数组中的字符串为
text=text.Replace(“\t”,”);//如下所示:“>”
string[]helper=text.Split(“”);
//List myContries=新列表();
列表j=新列表();
列表k=新列表();
for(int i=0;i>”){
Console.WriteLine(helper[i]);
Console.WriteLine(“添加到j”);
j、 加(i);
}

如果(helper[i]==”一个输入字符串为
“foo>>baz>>qux>>“
的快速测试按预期工作

在不知道输入是什么的情况下,我只能猜测,但有一种可能性是,
>
的其中一次出现没有在两侧用空格分隔,这意味着在拆分输入时它不会自动出现。

私有void FindCountries(字符串文本)
private void FindCountries(string text)
{
var words = text.Split(' ');
//List<string> myContries = new List<string>();
var tripleRights = new List<int>();
var doubleLefts = new List<int>();

for (var index = 0; index < words.Length; index++)
{
    if (String.Equals(words[index], ">>>", StringComparison.OrdinalIgnoreCase))
    {

        Console.WriteLine(words[index]);
        Console.WriteLine("found a triple right");
        tripleRights.Add(index);
    }

    if (String.Equals(words[index], "<<", StringComparison.OrdinalIgnoreCase))
    {

        Console.WriteLine(words[index]);
        Console.WriteLine("found a double left");
        doubleLefts.Add(index);
    }
}

Console.WriteLine("triple rights:");
foreach (var item in tripleRights)
{
    Console.WriteLine(item);
}
Console.WriteLine("double lefts");
foreach (var item in doubleLefts)
{
    Console.WriteLine(item);
}
}
{ var words=text.Split(“”); //List myContries=新列表(); var tripleRights=新列表(); var doublefts=新列表(); 对于(var索引=0;索引>>”,StringComparison.OrdinalIgnoreCase)) { Console.WriteLine(文字[索引]); Console.WriteLine(“找到三重权限”); 添加(索引); }
如果(String.Equals)(words[index],“您可以简单地执行以下操作:

string test = "23 <<< hello <<< to >> here <<<";

int found=0;
int seek_pos =0;

while (true) {
    var current = test.IndexOf("<<<", seek_pos);
    if (current == -1) 
        break;  
    else {
        Console.WriteLine("found one at position {0}, starting at: {1} ", current, seek_pos);
        found++;
        seek_pos = current + 3 ; // 3 chars in <<<
    }
}

Console.WriteLine("Total found: {0} ",found);

你可以试着调试它。@GrantWinney是的,请给我一分钟。@Mephy谢谢。这为我指明了正确的方向。是的,这很有效。我发现问题出在\r和\n上。你知道如何解决这些问题吗?这也很有效。我该如何为这个问题打分?@Mike每个答案旁边都应该有一个复选标记-你可以单击要接受的答案旁边的答案。
found one at position 3, starting at: 0 
found one at position 13, starting at: 6 
found one at position 28, starting at: 16 
Total found: 3