Search code examples
c#regexstringtrim

Remove All Instances of a Series at End of String


Suppose we have a program that returns a character array definition - so we can copy-paste it into some other C# code. One possible result is the following string:

// Current output:
return "CharArray = { {}, {123}, {}, {3 3}, {111}, {}, {}" + "};";

Now we'd like to eliminate the extra empty lines at the end of CharArray, while leaving any empty lines at the beginning or middle:

// Desired output:
"CharArray = { {}, {123}, {}, {3 3}, {111}" + "};";

(Any empty lines before or between data is necessary for spacing reasons, but empty space at the end serves no purpose to our code.)

Since the final bracket and semicolon aren't added until after the manipulation, it seems the easiest way to do this is to remove all trailing instances of ", {}" from the string. My current solution is a very outside-the-box combination of replaces and trims...

// Red-Green solution:
return output.Replace(", {}", "!").TrimEnd('!').Replace("!", ", {}") + "};";

...which certainly returns the correct result, but is lengthy, confusing to readers, and most likely caused you to cringe when you first read it.

Also, the Regex.Replace I would typically use for this sort of problem only removes one empty line, (because only one exists at the end of the string) and I'd rather not have to feed it through a loop:

// Sub-par solution: (birdie solution?)
 return Regex.Replace(testString, ", {}$", "") + "};";

How can I best remove all instances of a series of characters from only the end of a string? I'd prefer a result that is both readable and not too slow or taxing on the machine. (As far as the user can currently tell, the return is instantaneous after they press a button.)


Solution

  • you can use the regex:

    return "\n" + Regex.Replace(testString, "(, {})+$", "") + "};";
    

    this will replace also multiple occurences of the searched string

    the + operator means: one or multiple occurences of the preceding expression