Advertisement
ivandrofly

Remove Recursive Keep Count

Apr 6th, 2024
689
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.70 KB | None | 0 0
  1.         public static string RemoveRecursiveChar(string input, char ch, int keepCount)
  2.         {
  3.             var len = input.Length;
  4.             var chars = new char[len];
  5.             var writeIndex = 0;
  6.             var charCount = 0;
  7.             for (var j = 0; j < len; j++)
  8.             {
  9.                 if (input[j] != ch)
  10.                 {
  11.                     chars[writeIndex++] = input[j];
  12.                     charCount = 0;
  13.                 }
  14.                 else if (charCount + 1 <= keepCount)
  15.                 {
  16.                     chars[writeIndex++] = ch;
  17.                     charCount++;
  18.                 }
  19.             }
  20.  
  21.             return new string(chars, 0, writeIndex);
  22.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement