1

I am writing C# codes for Tabular Editor. I have a string Like "mkt_tag" and want to convert it into "Mkt Tag". I wrote this code but still have some problems. Please help me to out.

foreach(var obj in Selected.Columns) {
    var oldName = obj.Name; //"mkt_tag"
    var newName = new System.Text.StringBuilder();
    for(int i = 0; i < oldName.Length; i++) {
        // First letter should always be capitalized:
        if(i == 0) newName.Append(Char.ToUpper(oldName[i]));
        
        else if(oldName[i] == '_')
        {
            newName.Append(" ");
            newName.Append(Char.ToUpper(oldName[i+1]));
        }
      else
        {
            newName.Append(oldName[i]);
        }
        
    }
    obj.Name = newName.ToString();
    newName.Output();
}

Below is the current output:- "Mkt Ttag" enter image description here

  • Per first duplicate - `CultureInfo.CurrentCulture.TextInfo.ToTitleCase("mkt_tag" .Replace("_", " "))` or if your "split" behavior is more complicated use the other duplicate to split the string and than construct it back with `string.Join`. – Alexei Levenkov Apr 12 '22 at 05:23

0 Answers0