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

  • SEARCH
  • Home
  • 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

I would like to check if a given string has a correct html syntax.
I've been looking for a way to hash a given string in C# that
Algorithm to generate all possible letter combinations of given string down to 2 letters
what would be the regular expression to check if a given string contains atleast
How can I copy a substring from a given string with start and end
I need to add days to given string date and display calculated date in
I am trying to validate that the given string contains contains only letters, numbers,
So I'm trying to write a method that reverses a given string but the
Given String // 1 2 3 String a = letters.1223434.more_letters; I'd like to recognize
I would like to be able to check from python if a given string

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.