C# でスライス
C# で配列のスライスがあまりにも苦痛だったので作った. string も気がついたら作っていた. skip はめったに使わないので放置.
namespace MyExtensions { public static class Extension { public static T[] Slice<T>(this T[] source, int start, int end) { if (start < 0) start = source.Length + start; if (end < 0) end = source.Length + end; T[] result = new T[end - start]; Array.Copy(source, start, result, 0, end - start); return result; } public static T[] Slice<T>(this T[] source, int start) { return Slice(source, start, source.Length); } // XXX: サロゲートペアが正しく処理されない public static string Slice(this string source, int start, int end) { if (start < 0) start = source.Length + start; if (end < 0) end = source.Length + end; return source.Substring(start, end - start); } public static string Slice(this string source, int start) { return Slice(source, start, source.Length); } } } // For .NET Framework 2.0 Only namespace System.Runtime.CompilerServices { public class ExtensionAttribute : Attribute { } }