Adding a whitespace before uppercase letter in an FString

I can’t for the life of me figure out how to figure out if a given character in an FString is an uppercase letter, there doesn’t seem to be any language features that do this.

All I want is something that for example lets me turn the FString “HelloWorld” into “Hello World”, and I want to do it in C++.

I’m sure there are many better & more performant ways to do this (in fact I know Blueprints do this when exposing C++ variables in the Editor but I can’t find where this is implemented) but the following should work:

.h

FString ConvertPascalCaseToSentence(const FString string) const;

.cpp

FString YOURCLASS::ConvertPascalCaseToSentence(const FString input) const
{
	FString lower = input.ToLower();	

	if (input.Equals(lower, ESearchCase::CaseSensitive))
		return input;
	else
	{
		const TArray<TCHAR> inputArray = input.GetCharArray();
		const TArray<TCHAR> lowerArray = lower.GetCharArray();
		FString output;

		for (int i = 0; i < inputArray.Num(); i++)
		{
			if (i > 0 && inputArray[i] != lowerArray[i])
				output.AppendChar(' ');			

			output.AppendChar(inputArray[i]);
		}

		return  output;
	}	
}

FName::NameToDisplayString is what we use in the editor for this sort of thing.

3 Likes

Doh! This is the correct answer - not my reinvention of the triangular wheel above.

Oh jeez, of course it’s that easy… I never would’ve thought to look in FName for transformations on an FString, no wonder I didn’t find it. I would ask you to give it as an answer to the question, but I guess staff members don’t need those internet points, so I’ll just give it to staticvoidlol.