Download

TEXT("") variable as FString::Printf() parameter

Hi!
How could I use TEXT("") in a variable so I could use it as FString::Printf() parameter? I can’t understand what’s going on:


// Error: auto Format = Total < 100 ? TEXT("%02d/%02d") : TEXT("%03d/%03d");
// Error: FString Format = Total < 100 ? FString(TEXT("%02d/%02d")) : FString(TEXT("%03d/%03d"));
// Error: FText Format = Total < 100 ? FText::FromString(TEXT("%02d/%02d")) : FText::FromString(TEXT("%03d/%03d"));

FString FormattedText = FString::Printf(Format, Turn, TurnTotal);

The code below works, but I’d like to understand why the code above doesn’t:


if (Total < 100)
{
FString FormattedText = FString::Printf(TEXT("%02d/%02d"), Turn, TurnTotal);
}
else
{
FString FormattedText = FString::Printf(TEXT("%03d/%03d"), Turn, TurnTotal);
}

Thank you!

Like so:



FString::Printf(TEXT("MyText %s", "SomeOtherText"));

or

FString SomeOtherText = TEXT("Something");
FString::Printf(TEXT("MyText %s", *SomeOtherText));


Thank you @TheJamsh !

I didn’t explain it properly,
What I’d like to do is store the first FString::Printf() parameter in a variable, because it can have two values:



// **Error: auto Format** = Total < 100 ? TEXT("%02d/%02d") : TEXT("%03d/%03d");
// **Error: FString Format** = Total < 100 ? FString(TEXT("%02d/%02d")) : FString(TEXT("%03d/%03d"));
// **Error: FText Format** = Total < 100 ? FText::FromString(TEXT("%02d/%02d")) : FText::FromString(TEXT("%03d/%03d"));

FString FormattedText = **FString::Printf**(**Format**, Turn, TurnTotal);

But the code above doesn’t work.

What do you mean by “doesn’t work”? It would be helpful if you could add compiler error messages.

Also: Try making Format an FString and then do something like this, maybe?


FString::Printf(*Format, Turn, TurnTotal);

Edit: Keep in mind that if you work with FString, you need to dereference it to get access to the wchar_t* behind it.

Thank you @Kyoril ,
By doesn’t work I mean compiler error in the first parameter:

I don’t know how to use a variable with this parameter.

You can’t use FText like that in Printf. It is not what you think, FText is a struct completely unrelated to the TEXT macro.

FText has a ToString() function you can use, but as Bruno says FText is not really for this.

You can find info on UE4’s string types here:

I’m having the same problem.

This compiles:


FString formatted = FString::Printf(TEXT("test"), TEXT("123"));

This doesn’t:


const TCHAR* a = TEXT("test");
FString formatted = FString::Printf(a, TEXT("123"));

Same error as described avove.

Hi @DarthSomebody](User Profile - Unreal Engine Forums), @SolidSk](User Profile - Unreal Engine Forums)

Please, check FString::Format



const int32 MyInt = 12;
const float MyFloat = 0.12f;
const FString MyString = FString(TEXT("MyStringValue"));

//const FString MyStringPrintf = FString::Printf(TEXT("MyStringPrintf: {0}, {1}, {2}"));
const FString MyStringPrintf = FString(TEXT("MyStringPrintf: {0}, {1}, {2}"));

const FString MyStringFormatted = FString::Format(*MyStringPrintf, { MyInt, MyFloat, MyString });

UE_LOG(LogTemp, Warning, TEXT("MyStringFormatted: %s"), *MyStringFormatted);


My comment on Answerhub for the same question:
https://answers.unrealengine.com/que…comment-989170

Hope this helps.:slight_smile: