MS Excel formula to replace space

I am looking for an excel formula for following (Excel 2016 - If that ever matters)
    Prefix "Doc" to  cell A2 value
     Replace space in column A value by _   ( But if there are two or more space have only one _ )
     Write the value to cell B2

E.x  My Single space  should become My_Single_Space
       My  DoubleSpace    turns to  My_DoubleSpace       ( Even though there are two spaces, only one _)
       My   TripleSpace singlespace      turns to My_TripleSpace_SingleSpace
Sam OZAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Saqib Husain, SyedEngineerCommented:
=SUBSTITUTE(TRIM(A1)," ","_")

Change A1 to wherever your data is
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
NorieAnalyst Assistant Commented:
Perhaps.

="Doc"&SUBSTITUTE(TRIM(A2)," ","_")
0
Subodh Tiwari (Neeraj)Excel & VBA ExpertCommented:
Maybe this...
Assuming your string is in A2, then try this...
="Doc_"&SUBSTITUTE(SUBSTITUTE(TRIM(A2),CHAR(160),"")," ","_")

Open in new window

0
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

Sam OZAuthor Commented:
The real challenge with me is that whether it is one or two or three space, I should get only one _   . I don't think any of the solution is doin it
0
Saqib Husain, SyedEngineerCommented:
All of them do it. Send an example file showing your problem.
1
NorieAnalyst Assistant Commented:
The TRIM function, that all the suggested formulas use, will change multiple spaces within a string to single spaces.

If that's not happening something else must be going on, perhaps the 'spaces' aren't some other non-printing character.
0
Sam OZAuthor Commented:
My aplogies! that is right
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
VBA

From novice to tech pro — start learning today.