当然。
Regex.Split(input, @"(?=[aeiou])").Where(s => !string.IsNullOrWhiteSpace(s))
.OrderBy(n => n).Aggregate((a, b) => a + b);
如果你不想使用 Regex,我们需要一个扩展方法:
public static IEnumerable<IEnumerable<T>> SplitOn<T>(this IEnumerable<T> source, params T[] splitObjs)
{
//appropriate error checking, check for null etc
if (!source.Any() || !splitObjs.Any()) return new[]{source};
List<T> buffer = new List<T>()
foreach (T item in source)
{
if (splitObjs.Contains(item) && buffer.Any())
{
yield return buffer;
buffer.Clear();
}
buffer.Add(item);
}
if (buffer.Any()) yield return buffer;
}
然后它应该很简单:
input.SplitOn('a', 'b', 'c', 'd', 'e').Select(s => new string(s.ToArray()))
.OrderBy(n => n).Aggregate((a, b) => a + b);