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 1100581
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T00:54:10+00:00 2026-05-17T00:54:10+00:00

let’s say I have the following string: string s = A B C D

  • 0

let’s say I have the following string:

string s = "A B  C   D    TESTTEST  BLA BLA      TEST       TEST2"

Then I need the following:

"A B  C   D    TESTTEST  BLA BLA      TEST       TEST2"

So the rules are:
a) replace every second space (between non space chars) with  
b) If the last space is replaced with  , try to move it backwards (if possible) one step so the next word is not touched by the force space.

Background:

I want to use that to print data from the database to my website. But I want to optimize the forced spaces to use less space. I also want the last forced space not to touch the next word (if possible) so it is easier for some search engines to catch that word.

Will I need to loop through every char in that string and count the occurrence or is there an easier, faster and more fancy way?

Thank you, both solutions are working perfect.
So I did a benchmark to figure out which solution to accept:

@Guffa, your solution needs 22 seconds for 1 Million runs
@Timwi, your solution needs 7 seconds for 1 Million runs

I will give you both an upvote, but I will accept Timwi’s solution.

  • 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-05-17T00:54:11+00:00Added an answer on May 17, 2026 at 12:54 am

    Let’s use a regular expression!

    var input = "A B  C   D    TESTTEST  BLA BLA      TEST       TEST2";
    var output = Regex.Replace(input, @"  +", m =>
        m.Length == 2 ? "  " :
        m.Length % 2 == 1 ? "  ".Repeat(m.Length / 2) + " " :
        "  ".Repeat(m.Length / 2 - 1) + "  ");
    

    This uses a string.Repeat extension method I wrote:

    /// <summary>
    /// Concatenates the specified number of repetitions of the current string.
    /// </summary>
    /// <param name="input">The string to be repeated.</param>
    /// <param name="numTimes">The number of times to repeat the string.</param>
    /// <returns>A concatenated string containing the original string the specified number of times.</returns>
    public static string Repeat(this string input, int numTimes)
    {
        if (numTimes == 0) return "";
        if (numTimes == 1) return input;
        if (numTimes == 2) return input + input;
        var sb = new StringBuilder();
        for (int i = 0; i < numTimes; i++)
            sb.Append(input);
        return sb.ToString();
    }
    

    By the way, if by “saving space” you are referring to bandwidth / storage space, you can make this even more compact by using "\xa0" instead of "&nbsp;", which is equivalent in HTML.

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

Sidebar

Related Questions

Let's say you have a class called Customer, which contains the following fields: UserName
let's say i have three tables, each one relates to another, when i need
Let's say I have the following code: Sub TestRangeLoop() Dim rng As Range Set
Let's say I'm building a data access layer for an application. Typically I have
Let me try to explain what I need. I have a server that is
Let's say we have a simple function defined in a pseudo language. List<Numbers> SortNumbers(List<Numbers>
Let's say I have a drive such as C:\ , and I want to
Let's say that we have an ARGB color: Color argb = Color.FromARGB(127, 69, 12,
Let's say you have some products which have 2 fields, a description and a
Let's say I have a property foo defined in my parent POM. Is it

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.