String Handling

FName

When you name a new asset in the Content Browser, change a parameter in a Dynamic Material Instance, or access a bone in a Skeletal Mesh, you are using FNames. FNames provide a very lightweight system for using strings, where a given string is stored only once in a data table, even if it is reused. FNames are case-insensitive. They are immutable, and cannot be manipulated. The storage system and static nature of FNames means that it is fast to look up and access FNames with keys. Another feature of the FName subsystem is the use of a hash table to provide fast string to FName conversions.

FText

FText represents a display string. Any text you want to display to the user should be handled with FText. The FText class has built-in support for localization, and can handle text content that is localized and stored in a lookup table, as well as text that is localized at runtime, such as numbers, dates, times, and formatted text. Even text that does not need to be localized can be handled with FText. This includes user-entered content such as a player's name, and any text displayed with Slate. FText does not offer any mutation functions, because making changes to display strings is a very unsafe operation.

FString

Unlike FName and FText, FString is the only string class that allows for manipulation. There are many methods available for string manipulation, including case changing, excerpting substrings, and reversing. FStrings can be searched, modified, and compared against other strings. However, these manipulations can make FStrings more expensive than the immutable string classes.

Conversions

From To Example
FName FString TestHUDString = TestHUDName.ToString();
FName FText TestHUDText = FText::FromName(TestHUDName);

FName -> FText is valid in some cases, but be aware that the FNames's content will not benefit from the FText's "auto localization".

FString FName TestHUDName = FName(*TestHUDString);

FString -> FName is dangerous as the conversion is lossy as FName's are case insensitive.

FString FText TestHUDText = FText::FromString(TestHUDString);

FString -> FText is valid in some cases, but be aware that the FString's content will not benefit from the FText's "auto localization".

FText FString TestHUDString = TestHUDText.ToString();

FText -> FString is dangerous as it is a potentially lossy conversion for some languages.

FText FName There is no direct conversion from FText to FName. Instead, convert to FString and then to FName.

FText -> FString -> FName is dangerous as the conversion is lossy as FName's are case insensitive.

Encoding

In general, you should be using the TEXT() macro when setting string variable literals. If you do not specify the TEXT() macro, your literal will be encoded using ANSI, which is highly limited in what characters it supports. Any ANSI literals being passed into FString need to undergo a conversion to TCHAR (native Unicode encoding), so it is more efficient to use TEXT().

For more about encoding, see the Character Encoding documentation.