( Split(' ')) ( ):
public static IEnumerable<T[]> SlidingWindow<T>(this IEnumerable<T> source,
int windowSize) {
if (null == source)
throw new ArgumentException("source");
else if (windowSize <= 0)
throw new ArgumentOutOfRangeException("windowSize",
"Window size must be positive value");
List<T> window = new List<T>(windowSize);
foreach (var item in source) {
if (window.Count >= windowSize) {
yield return window.ToArray();
window.RemoveAt(0);
}
window.Add(item);
}
if (window.Count > 0)
yield return window.ToArray();
}
SlidingWindow , , , - ( ).
var sentence = "Regex for taking out words out of a string from a specific position";
var result = Regex
.Matches(sentence, @"[\w\-]+")
.OfType<Match>()
.Select(match => match.Value)
.SlidingWindow(3);
var test = String.Join(Environment.NewLine, result
.Select(line => $"[{string.Join(" ", line)}]"));
Console.Write(test);
[Regex for taking]
[for taking out]
[taking out words]
[out words out]
[words out of]
[out of a]
[of a string]
[a string from]
[string, from, a]
[from a specific]
[a specific position]
, , Split, :
// Split solution: as usual + final representation as sliding window
var result = sentence
.Split(' ') // just split...
.SlidingWindow(3); // ... and represent as sliding windows