[Solved] Separate object string when it’s an uppercase letter c# [duplicate]


Is Regex required? Here’s linq.

edit: added a regex option as well since that was requested.

([a-z])([A-Z])", "$1 $2") matches lowercase letter and then uppercase letter and returns the matches as $1 and $2 respectively w/ a space in between.

Note: this won’t work if you have accented characters like É, is that needed in your case?

    string input = "HelloWorldHowAreYouToday";
    string output = string.Concat(input.Select(x => Char.IsUpper(x) ? " " + x : x.ToString())).TrimStart(' ');
    Console.WriteLine(output);
    
    //regex example.
    string regExResult = Regex.Replace(input, @"([a-z])([A-Z])", "$1 $2");
    Console.WriteLine(regExResult);

output for both: Hello World How Are You Today

1

solved Separate object string when it’s an uppercase letter c# [duplicate]