Skip to content

Instantly share code, notes, and snippets.

@NickStrupat
Last active October 2, 2018 18:09
Show Gist options
  • Save NickStrupat/6bb888d7ee5298fcd8e52dddf401cab6 to your computer and use it in GitHub Desktop.
Save NickStrupat/6bb888d7ee5298fcd8e52dddf401cab6 to your computer and use it in GitHub Desktop.
Split sentence into all possible combinations of sub-sentences
using System;
using System.Collections.Generic;
using System.Linq;
namespace WhaHappen
{
public static class StringExtensions
{
public static IEnumerable<String> SplitIntoAll(this String s, Char delimiter = ' ')
{
return s.SplitIntoAll(delimiter.ToString());
}
public static IEnumerable<String> SplitIntoAll(this String s, String delimiter = " ")
{
var tokens = s.Split(delimiter, StringSplitOptions.RemoveEmptyEntries);
for (var i = 0; i != tokens.Length; i++)
for (var j = 1; j != tokens.Length - i; j++)
yield return String.Join(delimiter, tokens.Skip(i).Take(j));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment