In my MVC project, I am getting dropdown text data as "NoEffect", "FadeOut" etc, now I would like to show user as "No Effect", "Fade out" etc, show how can I achieve it using C#?
Any method would work, using regex or without using regex.
Try using this below Linq code(without using regex) to add space before capital letters
var YourString= "NoEffect";
//get output as "No Effect"
@string.Concat(YourString.Select(x => Char.IsUpper(x) ? " " + x : x.ToString())).TrimStart(' ');
This should work.
Here is the simple program to add space before capital letters using regex in C#
using System;
using System.Text.RegularExpressions;
public class Program
{
public static void Main()
{
var oldvalue= "ThisIsTest";
var newValue = Regex.Replace(oldvalue, "([a-z])([A-Z])", "$1 $2");
Console.WriteLine(newValue);
}
}
Output:
This Is Test
You can check the fiddle here https://dotnetfiddle.net/4V0v70 but above Linq Solutions looks easy and simple to me.
If you want to create Extension method to add space before capital
public static class Extensions
{
public static string SpaceBeforeCapital( this string Input )
{
return new string(Input.SelectMany((c, i) => i > 0 && char.IsUpper(c) ? new[] { ' ', c } : new[] { c }).ToArray());
}
}
Now you can use it like MyCasedString.SpaceBeforeCapital()
this.
Subscribe to our weekly Newsletter & Keep getting latest article/questions in your inbox weekly