StringExtensions.cs 1.5 KB

12345678910111213141516171819202122232425
  1. using System;
  2. namespace NLangDetect.Core.Extensions
  3. {
  4. public static class StringExtensions
  5. {
  6. /// <summary>
  7. /// Returns a new character sequence that is a subsequence of this sequence. The subsequence starts with the character at the specified index and ends with the character at index end - 1. The length of the returned sequence is end - start, so if start == end then an empty sequence is returned.
  8. /// </summary>
  9. /// <param name="s"></param>
  10. /// <param name="start">the start index, inclusive</param>
  11. /// <param name="end">the end index, exclusive</param>
  12. /// <returns>the specified subsequence</returns>
  13. /// <exception cref="IndexOutOfRangeException"> if start or end are negative, if end is greater than length(), or if start is greater than end</exception>
  14. public static string SubSequence(this string s, int start, int end)
  15. {
  16. if (start < 0) throw new ArgumentOutOfRangeException(nameof(start), "Argument must not be negative.");
  17. if (end < 0) throw new ArgumentOutOfRangeException(nameof(end), "Argument must not be negative.");
  18. if (end > s.Length) throw new ArgumentOutOfRangeException(nameof(end), "Argument must not be greater than the input string's length.");
  19. if (start > end) throw new ArgumentOutOfRangeException(nameof(start), "Argument must not be greater than the 'end' argument.");
  20. return s.Substring(start, end - start);
  21. }
  22. }
  23. }