1
private static string SetValue(string input, string reference)
{
    string[] sentence = input.Split(' ');
    for(int word = 0; word<sentence.Length; word++)
    {
        if (sentence[word].Equals(reference, StringComparison.OrdinalIgnoreCase))
        {
            return String.Join(" ", sentence.subarray(word+1,sentence.Length))
        }
    }
}

我怎样才能sentence.subarray(word+1,sentence.Length)轻松完成或以其他方式做到这一点?

4

4 回答 4

5

String.Join有一个专门为此而重载:

return String.Join(" ", sentence, word + 1, sentence.Length - (word + 1));
于 2013-02-18T10:56:43.570 回答
1

如果您正在严格寻找独立于 string.Join() 函数的子数组解决方案,并且您使用的是支持 Linq 的 .NET 版本,那么我可以推荐:

sentence.Skip(word + 1);
于 2013-02-18T11:00:54.513 回答
1

Where您可以使用重载index

return string.Join(" ", sentence.Where((w, i) => i > word));
于 2013-02-18T11:17:17.207 回答
0

或者,您可以使用SkipWhile而不是 for 循环。

private static string SetValue(string input, string reference)
{
    var sentence = input.Split(" ");
    // Skip up to the reference (but not the reference itself)
    var rest = sentence.SkipWhile( 
        s => !s.Equals(reference, StringComparison.OrdinalIgnoreCase));
    rest = rest.Skip(1); // Skip the reference
    return string.Join(" ", rest);
}
于 2013-02-18T10:59:52.503 回答