C# 如果foreach循环中的一个单词包含xxx,则显示上一个单词

C# 如果foreach循环中的一个单词包含xxx,则显示上一个单词,c#,string,C#,String,如果一个单词与我比较的字符串匹配,我希望看到我检查的前一个单词。听起来很难理解,所以我希望代码能更好地解释: String line = "qwerty/asdfg/xxx/zxcvb"; string[] words = line.Split('/'); foreach (string word in words) { if (word.Contains("xxx")) { Console.WriteLine(word.Last); Consol

如果一个单词与我比较的字符串匹配,我希望看到我检查的前一个单词。听起来很难理解,所以我希望代码能更好地解释:

String line = "qwerty/asdfg/xxx/zxcvb";
string[] words = line.Split('/');
foreach (string word in words)
{
    if (word.Contains("xxx"))
    {
        Console.WriteLine(word.Last);
        Console.Writeline(word.Next); //It doenst work this way, but I hope you understand the idea
    }
}

您需要做的是使用一个变量来维护搜索的当前状态。在您的情况下,它非常简单,因为您只需要记住最后一个单词:

string line = "qwerty/asdfg/xxx/zxcvb";
string[] words = line.Split('/');
string previousWord = "";
foreach (string word in words)
{
    if (word.Contains("xxx"))
    {
        Console.WriteLine(previousWord);
    }
    previousWord = word;
}

只需存储您检查的最后一个单词

此外,如果您需要了解区域性或区分大小写,则应该使用
String.IndexOf()

String line = "qwerty/asdfg/xxx/zxcvb";
string[] words = line.Split('/');
string previous = words.First();
string next = "";

foreach (string word in words)
{
    var currentIndex = (Array.IndexOf(words, word));
    if (currentIndex + 1  < words.Length)
    {
        next = words[currentIndex + 1];
    }

    if (word.IndexOf("xxx", StringComparison.CurrentCulture) > -1)
    {
        Console.WriteLine(previous);

        if (next == word) 
        {
            Console.WriteLine("The match was the last word and no next word is available.");
        }
        else 
        {
            Console.WriteLine(next);
        }
    }
    else 
    {
        previous = word;
    }
}
String line=“qwerty/asdfg/xxx/zxcvb”;
string[]words=line.Split('/');
字符串previous=words.First();
字符串next=“”;
foreach(单词中的字符串)
{
var currentIndex=(Array.IndexOf(words,word));
if(当前索引+1<字数长度)
{
next=字[currentIndex+1];
}
if(word.IndexOf(“xxx”,StringComparison.CurrentCulture)>-1)
{
控制台写入线(上一个);
如果(下一个==单词)
{
WriteLine(“匹配是最后一个单词,没有下一个单词可用。”);
}
其他的
{
控制台写入线(下一个);
}
}
其他的
{
上一个=单词;
}
}
在这种特殊情况下,“asdfg”作为前一个输出,“zxcvb”作为下一个输出。但是,如果您将“zxcvb”作为匹配项,则会输出“匹配项是最后一个单词,没有下一个单词可用”。

String line=“xxx/qwerty/asdfg/xxx/zxcvb”;
 String line = "xxx/qwerty/asdfg/xxx/zxcvb";
 string[] words = line.Split('/');
  for (int i = 0; i < words.Length; i++)
   {
       if (words[i].Contains("xxx"))
        {
                Console.WriteLine(words[i]); //current word
                if (i == 0)
                    Console.WriteLine("No Previous Word");
                else
                    Console.WriteLine(words[i - 1]); //PreviousWord

                if (i == words.Length - 1)
                    Console.WriteLine("No Next Word");
                else                                           
                    Console.WriteLine(words[i + 1]); //NextWord
         }
    }
string[]words=line.Split('/'); for(int i=0;i
单词[words.IndexOf(word)-1]应该可以做到这一点


编辑:忘记这个。Almo更好,也不容易出现异常。

只需使用
for
循环,而不是
foreach

String line = "qwerty/asdfg/xxx/zxcvb";

string[] words = line.Split('/');
for (int i = 0; i < words.Length; i++)
{
    if (words[i].Contains("xxx"))
    {
        // Check to make sure you to go beyond the first element
        if (i - 1 > -1)
        {
            // Previous word
            Console.WriteLine(words[i - 1]);
        }

        // Check to make sure you to go beyond the last element    
        if (i + 1 < words.Length)
        {
            // Next word
            Console.WriteLine(words[i + 1]);
        }
    }
}

Console.ReadLine();
无环 您还可以使用
Array.IndexOf()

String line=“qwerty/asdfg/xxx/zxcvb”;
string[]words=line.Split('/');
int index=Array.IndexOf(单词“xxx”);
//检查以确保超出第一个元素
如果(索引-1>-1)
{
//前文
Console.WriteLine(单词[index-1]);
}
//检查以确保超出最后一个元素
如果(索引!=-1&&index+1
Genius。你能为下一个单词编辑代码吗D@wouter当前位置我觉得你有点贪婪。你不觉得吗?您也需要付出一些努力。@sstan您是对的,添加下一个单词应该很容易,我为我的贪婪感到抱歉,无论如何,谢谢您的回答@Almo!:DBad索引异常如果第一个单词包含“xxx”,那么如果最后一个单词包含
xxx
@bill:这不会修复它,那么它也会在最后一个单词上引发异常。索引越界不会返回null,它会抛出一个IndexOutfrange例外答案是固定的,并且是提供下一个单词的唯一解决方案,正如OP askedI所看到的,除了你的好答案,我觉得使用Almo的答案tho更容易,因为它有点短和清晰:)如果它不起作用,我将使用你的答案tho,谢谢你帮我解决这个问题!:)查看我对上一个和下一个单词的回答我没有投反对票,但我很好奇:如果有重复的
word
,你的东西怎么工作?
IndexOf
是否通过引用进行操作?我想会的。重复的被忽略了。首先出现“单词”。但Almo's也是如此——没有出现IndexOutofrage例外的危险。但是你很好奇:你想如何处理可能的重复?我的处理bloof/xxx/flarg/xxx很好。它先打印“bloof”,然后打印“flarg”。如果IndexOf(“xxx”)始终返回第一个“xxx”,它将打印两次“bloof”。但我怀疑它找到的是引用的索引,而不是值的索引。我的索引应该返回值而不是引用。现在无法在iPhone上测试。问题仍然存在:如何处理副本?如果需要这样处理的话,你的电脑可以很好地处理它们。需要澄清。
asdfg
zxcvb
String line = "qwerty/asdfg/xxx/zxcvb";

string[] words = line.Split('/');
int index = Array.IndexOf(words, "xxx");

// Check to make sure you to go beyond the first element
if (index - 1 > -1)
{
    // Previous word
    Console.WriteLine(words[index - 1]);
}

// Check to make sure you to go beyond the last element    
if (index != -1 && index + 1 < words.Length)
{
    // Next word
    Console.WriteLine(words[index + 1]);
}

Console.ReadLine();