Click here to Skip to main content
15,917,702 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hi,

I have a string that looks like this:
"ThisIsSoCool"

I would like to know how to insert spaces whenever there is an uppercase in the string?

I tried this code but it replaces the uppercases with blanks:

C#
static string returnStringWithSpaces(ref string s)
        {
            System.Text.RegularExpressions.RegexOptions options = new System.Text.RegularExpressions.RegexOptions();
            options |= System.Text.RegularExpressions.RegexOptions.None;
            s = System.Text.RegularExpressions.Regex.Replace(s, "[A-Z]", " ", options);
            return s;
        }



Please advise

Thank you
Posted

1 solution

Try:
MIDL
string s = "IsThisWorking?";
s = System.Text.RegularExpressions.Regex.Replace(s, "[A-Z]", "$& ");
- the '$&' part inserts the entire match in the output.
Get a copy of Expresso[^] - it's free and makes working with regular expressions a lot easier!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900