Added an extension method for splitting messages for sending long multi-line messages

This commit is contained in:
barelyprofessional
2024-09-17 00:55:06 +08:00
parent d20adeb6ef
commit 4753783c41

View File

@@ -27,4 +27,21 @@ public static class Extensions
return s;
}
public static IEnumerable<string> SplitMessage(this string s, int partLength = 500, int partLimit = 5)
{
if (s == null)
throw new ArgumentNullException(nameof(s));
if (partLength <= 0)
throw new ArgumentException("Part length has to be positive.", nameof(partLength));
var parts = 0;
for (var i = 0; i < s.Length; i += partLength)
{
parts++;
if (parts > partLimit) break;
yield return s.Substring(i, Math.Min(partLength, s.Length - i));
}
}
}