如何在c#中获得即时价值

本文关键字: | 更新日期: 2023-09-27 18:08:19

string[] str = new string[] {"sun", "mon", "tue", "wed", "thu", "fri","sat"};

如果我传递Mon,那么结果集应该立即值为true。

如何在c#中获得即时价值

如果您想立即获得后面的值,请尝试以下操作:

string sIn = "mon";
string[] str = new string[] {"sun", "mon", "tue", "wed", "thu", "fri", "sat"};
int current = Array.IndexOf(str, sIn);
int next = (current + 1) % str.Length;
string sOut = str[next];
return sOut;

您需要检查数组是否包含输入字符串。

string[] str = new string[] {"sun", "mon", "tue", "wed", "thu", "fri","sat"};
public string NextDay(string day)
{
    string result = "";
    int index = Array.IndexOf(str, day);
    if(index > -1)
    {
        result = str[(index + 1) % str.Length];
    }
    else
    {
        result = "input is wrong";
    }
    return result;
}

嗯,我假设你想返回下一个值?如果有的话,像这样

public string GetNext(string input)
{
    string[] str = new string[] {"sun", "mon", "tue", "wed", "thu", "fri","sat"};
    for(int I=0; I < str.length; I++)
    {
        if(str[I] == input)
        {
            return str[I+1];
        }
    }
}

你需要一些错误检查,如果你想包装,使返回太阳。如下所示

public string GetNext(string input)
{
    string[] str = new string[] {"sun", "mon", "tue", "wed", "thu", "fri","sat"};
    for(int I=0; I < str.length; I++)
    {
        if(str[I] == input)
        {
            if(I == str.length-1)
            {
                return str[0];
            } else {
                return str[I+1];
            }
        }
    }
}

Like This,

string[] str = new string[] { "sun", "mon", "tue", "wed", "thu", "fri", "sat" };    
var result = Array.FindIndex(str, element => element.Equals("mon", StringComparison.Ordinal));      
System.Console.WriteLine(result==-1 ? "not found" : str[result+1]);
相关文章:
  • 没有找到相关文章