title case

How do I change a string to title case? I can change it to Upper or Lower, and capitalize the first letter by combining those using Substring(), but it doesn’t work on strings that have multiple words.

Hi @lindsey,

You certainly have options - what part of the application are a working in?

Most data visualization controls have Text properties to allow you to change the text directly to Uppercase/Lowercase.

In a Data Cube you could also use the String transform to set a specific field.

Hope this helps!

That doesn’t get me title case. I have upper and lower, but I need title case. Thanks!

Thanks for the text transform screenshot though, I didn’t realize I could do this in the metric set and not the data cube! For this one I’m fine sticking with the data cube though.

Currently I have the following calculated field in the cube, but It doesn’t work on values that have more than one word:

if ($myField$ != null) return $myField$.ToUpper().Substring(0,1) + $myField$.ToLower().Substring(1); else return null;

The only thing I can think is that it would be possible using RegEx to split based on word boundaries, but I was hoping there’s an easier way! I know there’s a ToTitleCase() in .NET documentation, but it’s not available for this.

Hi @lindsey - you’re right; that’s not title case. I shouldn’t answer these before a suitable amount of coffee!!!.

Try something like this as a Calculated Element Transform in the Data Cube.

string s = $Continent$;
if (s == null) return s;

    String[] words = s.Split(' ');
    for (int i = 0; i < words.Length; i++)
    {
        if (words[i].Length == 0) continue;

        Char firstChar = Char.ToUpper(words[i][0]); 
        String rest = "";
        if (words[i].Length > 1)
        {
            rest = words[i].Substring(1).ToLower();
        }
        words[i] = firstChar + rest;
    }
    return String.Join(" ", words);

3 Likes

That works, thank you!!

1 Like

Good day to all of you!

Depending on the culture settings of your system you can also simply use the Title Case command.

Here is what we use in calculated elements in our data cubes:

tempString = CultureInfo.CurrentCulture.TextInfo.ToTitleCase($Appl_Measure_Field_Value$.ToLower());

return tempString;

So basically we use the String.ToLower method to convert our string field to be all lower case and then we put it inside of the TextInfo.ToTitleCase( ) command which can be found in CultureInfo.CurrentCulture to put it back to TitleCase.

Have a great day everyone!

Tom

3 Likes