Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Diagnostics;
- namespace SplitOnLength
- {
- static class StringSplitter
- {
- public static IEnumerable<string> SaeedsOriginalApproach(this string str, int allowedLength)
- {
- var ret1 = str.Split(' ');
- var ret2 = new List<string>();
- ret2.Add("");
- int index = 0;
- foreach (var item in ret1)
- {
- if (item.Length + 1 + ret2[index].Length <= allowedLength)
- {
- ret2[index] += ' ' + item;
- if (ret2[index].Length >= allowedLength)
- {
- ret2.Add("");
- index++;
- }
- }
- else
- {
- ret2.Add(item);
- index++;
- }
- }
- return ret2;
- }
- public static IEnumerable<string> SaeedsApproach(this string str, int allowedLength)
- {
- var ret1 = str.Split(' ');
- string current = "";
- foreach (var item in ret1)
- {
- if (item.Length + 1 + current.Length <= allowedLength)
- {
- current += ' ' + item;
- if (current.Length >= allowedLength)
- {
- yield return current;
- current = "";
- }
- }
- else
- {
- yield return current;
- current = "";
- }
- }
- }
- public static IEnumerable<string> SplitOnLength(this string input, int length, bool maintainWords)
- {
- int index = 0;
- while (index < input.Length)
- {
- int stepsBackward = 0;
- if (index + length < input.Length)
- {
- if (maintainWords)
- {
- yield return GetBiggestAllowableSubstring(input, index, length, out stepsBackward);
- }
- else
- {
- yield return input.Substring(index, length);
- }
- }
- else
- {
- yield return input.Substring(index);
- }
- index += (length - stepsBackward);
- }
- }
- static string GetBiggestAllowableSubstring(string input, int index, int length, out int stepsBackward)
- {
- stepsBackward = 0;
- int lastIndex = index + length - 1;
- if (!char.IsWhiteSpace(input[lastIndex + 1]))
- {
- int adjustedLastIndex = input.LastIndexOf(' ', lastIndex, length);
- stepsBackward = lastIndex - adjustedLastIndex;
- lastIndex = adjustedLastIndex;
- }
- if (lastIndex == -1)
- {
- throw new ArgumentOutOfRangeException("The input string contains at least one word greater in length than the specified length.");
- }
- return input.Substring(index, lastIndex - index + 1);
- }
- }
- class Program
- {
- static void Main(string[] args)
- {
- string s = "This is a sample block of text that I would pass through the string splitter.";
- Console.WriteLine("Testing SplitOnLength(10):");
- foreach (string str in s.SplitOnLength(10, true))
- {
- Console.WriteLine(str);
- }
- Console.WriteLine();
- Console.WriteLine("Testing SaeedsOriginalApproach(10):");
- foreach (string str in s.SaeedsOriginalApproach(10))
- {
- Console.WriteLine(str);
- }
- Console.WriteLine();
- Console.WriteLine("Testing SaeedsApproach(10):");
- foreach (string str in s.SaeedsApproach(10))
- {
- Console.WriteLine(str);
- }
- Console.WriteLine();
- Console.WriteLine("Finished. Press Enter to continue.");
- Console.ReadLine();
- // This is the entire text of Kafka's 'In the Penal Colony',
- // as a sample input of significant length.
- string longInput = SplitOnLength.Properties.Resources.InThePenalColony;
- // This is an excerpt of the text of the OP's question,
- // as a sample input of small-medium length.
- string shortInput = SplitOnLength.Properties.Resources.QuestionText;
- do
- {
- try
- {
- Console.Write("Enter a split size: ");
- int length = int.Parse(Console.ReadLine());
- Console.WriteLine();
- Console.WriteLine("Testing my method...");
- Console.WriteLine();
- TimeSpan splitOnLengthTime = TestSplitMethod(longInput, length, (str, len) => str.SplitOnLength(len, true));
- TimeSpan saeedsOriginalApproachTime = TestSplitMethod(longInput, length, (str, len) => str.SaeedsOriginalApproach(len));
- TimeSpan saeedsApproachTime = TestSplitMethod(longInput, length, (str, len) => str.SaeedsApproach(len));
- Console.WriteLine("Results from longer input:");
- Console.WriteLine("SplitOnLength: {0} ms", splitOnLengthTime.TotalMilliseconds);
- Console.WriteLine("SaeedsOriginalApproach: {0} ms", saeedsOriginalApproachTime.TotalMilliseconds);
- Console.WriteLine("SaeedsApproach: {0} ms", saeedsApproachTime.TotalMilliseconds);
- splitOnLengthTime = TestSplitMethod(shortInput, length, (str, len) => str.SplitOnLength(len, true));
- saeedsOriginalApproachTime = TestSplitMethod(shortInput, length, (str, len) => str.SaeedsOriginalApproach(len));
- saeedsApproachTime = TestSplitMethod(shortInput, length, (str, len) => str.SaeedsApproach(len));
- Console.WriteLine();
- Console.WriteLine("Results from shorter input:");
- Console.WriteLine("SplitOnLength: {0} ms", splitOnLengthTime.TotalMilliseconds);
- Console.WriteLine("SaeedsOriginalApproach: {0} ms", saeedsOriginalApproachTime.TotalMilliseconds);
- Console.WriteLine("SaeedsApproach: {0} ms", saeedsApproachTime.TotalMilliseconds);
- }
- catch (Exception ex)
- {
- Console.WriteLine(ex.Message);
- }
- finally
- {
- Console.WriteLine();
- Console.Write("Go again? ");
- Console.WriteLine();
- }
- }
- while (Console.ReadLine().StartsWith("Y", StringComparison.OrdinalIgnoreCase));
- }
- static TimeSpan TestSplitMethod(string input, int length, Func<string, int, IEnumerable<string>> splitter)
- {
- // Ensure results aren't thrown off by garbage collections affecting
- // certain runs more or less harshly than others.
- GC.Collect();
- Stopwatch stopwatch = Stopwatch.StartNew();
- IEnumerable<string> split = splitter(input, length);
- foreach (string s in split)
- { }
- stopwatch.Stop();
- return stopwatch.Elapsed;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement