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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T20:18:51+00:00 2026-05-10T20:18:51+00:00

Given the string ThisStringHasNoSpacesButItDoesHaveCapitals what is the best way to add spaces before the

  • 0

Given the string ‘ThisStringHasNoSpacesButItDoesHaveCapitals’ what is the best way to add spaces before the capital letters. So the end string would be ‘This String Has No Spaces But It Does Have Capitals’

Here is my attempt with a RegEx

System.Text.RegularExpressions.Regex.Replace(value, '[A-Z]', ' $0') 
  • 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. 2026-05-10T20:18:52+00:00Added an answer on May 10, 2026 at 8:18 pm

    The regexes will work fine (I even voted up Martin Browns answer), but they are expensive (and personally I find any pattern longer than a couple of characters prohibitively obtuse)

    This function

    string AddSpacesToSentence(string text, bool preserveAcronyms) {         if (string.IsNullOrWhiteSpace(text))            return string.Empty;         StringBuilder newText = new StringBuilder(text.Length * 2);         newText.Append(text[0]);         for (int i = 1; i < text.Length; i++)         {             if (char.IsUpper(text[i]))                 if ((text[i - 1] != ' ' && !char.IsUpper(text[i - 1])) ||                     (preserveAcronyms && char.IsUpper(text[i - 1]) &&                       i < text.Length - 1 && !char.IsUpper(text[i + 1])))                     newText.Append(' ');             newText.Append(text[i]);         }         return newText.ToString(); } 

    Will do it 100,000 times in 2,968,750 ticks, the regex will take 25,000,000 ticks (and thats with the regex compiled).

    It’s better, for a given value of better (i.e. faster) however it’s more code to maintain. "Better" is often compromise of competing requirements.

    Update
    It’s a good long while since I looked at this, and I just realised the timings haven’t been updated since the code changed (it only changed a little).

    On a string with ‘Abbbbbbbbb’ repeated 100 times (i.e. 1,000 bytes), a run of 100,000 conversions takes the hand coded function 4,517,177 ticks, and the Regex below takes 59,435,719 making the Hand coded function run in 7.6% of the time it takes the Regex.

    Update 2 Will it take Acronyms into account? It will now! The logic of the if statment is fairly obscure, as you can see expanding it to this …

    if (char.IsUpper(text[i]))     if (char.IsUpper(text[i - 1]))         if (preserveAcronyms && i < text.Length - 1 && !char.IsUpper(text[i + 1]))             newText.Append(' ');         else ;     else if (text[i - 1] != ' ')         newText.Append(' '); 

    … doesn’t help at all!

    Here’s the original simple method that doesn’t worry about Acronyms

    string AddSpacesToSentence(string text) {         if (string.IsNullOrWhiteSpace(text))            return "";         StringBuilder newText = new StringBuilder(text.Length * 2);         newText.Append(text[0]);         for (int i = 1; i < text.Length; i++)         {             if (char.IsUpper(text[i]) && text[i - 1] != ' ')                 newText.Append(' ');             newText.Append(text[i]);         }         return newText.ToString(); } 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there a way to determine the display length of a given string (in
Given a string like this: a,string, with,various,values, and some,quoted What is a good algorithm
Given a string file path such as /foo/fizzbuzz.bar , how would I use bash
given a string: msg=hello world How can I define this as a ctypes.c_void_p() data
Given a sub-string, is there a way to generate all the possible regular expressions
Im looking for an elegant way in Scala to split a given string into
Given string s = '(A /something_1)(B /something_2)(C /something_3),/,(D /something_4)(D /something_5)' I would like to
Currently my ListView is filling up with the given String[] but I wanted to
How can I drop all tables whose names begin with a given string? I
I have a requirement to disallow backslash characters in a given string field defined

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.