From 4753783c41c20910133d396282c7cf7c81e335b5 Mon Sep 17 00:00:00 2001 From: barelyprofessional <150058423+barelyprofessional@users.noreply.github.com> Date: Tue, 17 Sep 2024 00:55:06 +0800 Subject: [PATCH] Added an extension method for splitting messages for sending long multi-line messages --- KfChatDotNetBot/Extensions.cs | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/KfChatDotNetBot/Extensions.cs b/KfChatDotNetBot/Extensions.cs index 5918621..27b507b 100644 --- a/KfChatDotNetBot/Extensions.cs +++ b/KfChatDotNetBot/Extensions.cs @@ -27,4 +27,21 @@ public static class Extensions return s; } + + public static IEnumerable 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)); + } + } + } \ No newline at end of file