Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8300819
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T16:41:47+00:00 2026-06-08T16:41:47+00:00

Suppose we have a program that returns a character array definition – so we

  • 0

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.)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T16:41:51+00:00Added an answer on June 8, 2026 at 4:41 pm

    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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I have a program that initializes a global variable for use by threads,
Suppose I have a big program that consists of hundreds of methods in it.
I have a requirement such that my program is suppose to run for a
I have a small program that's supposed to sample some value from a device
Suppose I have a pure virtual method in the base interface that returns to
Suppose I have a static method of my class that returns an object of
Suppose you have a disease diagnosis Prolog program that starts with many relations between
I am using Visual Studio 2010 and suppose I have to write some program.
I have a program that is supposed to interact with a web server and
I have a program for the iPhone that is supposed to be doing intelligent

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.