gpt הציע את זה
private void getSnippets()
{
List<string> snippets = SplitStringIntoSnippets(longText, 30);
foreach (string snippet in snippets)
{
//do something
}
}
static List<string> SplitStringIntoSnippets(string input, int maxSnippetLength)
{
List<string> snippets = new List<string>();
string[] words = input.Split(new char[] { ' ', '\t', '\n' }, StringSplitOptions.RemoveEmptyEntries);
int wordCount = 0;
int currentIndex = 0;
for (int i = 0; i < words.Length; i++)
{
wordCount++;
if (wordCount >= maxSnippetLength)
{
snippets.Add(string.Join(" ", words, currentIndex, maxSnippetLength));
currentIndex = i + 1;
wordCount = 0;
}
}
if (currentIndex < words.Length)
{
snippets.Add(string.Join(" ", words, currentIndex, words.Length - currentIndex));
}
return snippets;
}
