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)
或以其他方式执行此操作?
答案 0 :(得分:5)
String.Join
专门针对此an overload:
return String.Join(" ", sentence, word + 1, sentence.Length - (word + 1));
答案 1 :(得分:1)
如果您正在寻找独立于string.Join()函数的Subarray解决方案,并且您正在使用支持Linq的.NET版本,那么我可以推荐:
sentence.Skip(word + 1);
答案 2 :(得分:1)
您可以对index
使用重载Where
:
return string.Join(" ", sentence.Where((w, i) => i > word));
答案 3 :(得分: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);
}