StringExtensions.cs 860 B

1234567891011121314151617181920212223242526272829303132
  1. using System.Text;
  2. namespace Terminal.Gui {
  3. /// <summary>
  4. /// Extension helper of <see cref="System.String"/> to work with specific text manipulation./>
  5. /// </summary>
  6. public static class StringExtensions {
  7. /// <summary>
  8. /// Repeats the <paramref name="instr"/> <paramref name="n"/> times.
  9. /// </summary>
  10. /// <param name="instr">The text to repeat.</param>
  11. /// <param name="n">Number of times to repeat the text.</param>
  12. /// <returns>
  13. /// The text repeated if <paramref name="n"/> is greater than zero,
  14. /// otherwise <see langword="null"/>.
  15. /// </returns>
  16. public static string Repeat (this string instr, int n)
  17. {
  18. if (n <= 0) {
  19. return null;
  20. }
  21. if (string.IsNullOrEmpty (instr) || n == 1) {
  22. return instr;
  23. }
  24. return new StringBuilder (instr.Length * n)
  25. .Insert (0, instr, n)
  26. .ToString ();
  27. }
  28. }
  29. }