String Handling

Overview

FName

内容浏览器 中为新资源命名时,变更动态材质实例中的参数或访问骨骼网格体中的一块骨骼时需要使用 FNames。FName 通过一个轻型系统使用字符串。在此系统中,特定字符串即使会被重复使用,在数据表中也只存储一次。FNames 不区分大小写。它们为不可变,无法被操作。FNames 的存储系统和静态特性决定了通过键进行 FNames 的查找和访问速度较快。FName 子系统的另一个功能是使用散列表为 FName 转换提供快速字符串。

FText

FText 代表一个显示字符串。为用户显示的文本都需要由 FText 进行处理。FText 类拥有本地化的内置支持,可处理已本地化并存储在查找表中的文本内容,以及在运行时被本地化的文本,如数字、日期、时间和格式化文本。甚至可用 FText 处理无需进行本地化的文本。包括用户输入内容,如玩家名和 Slate 显示的文本。FText 不提供任何变异函数,因为对显示字符串进行修改是非常不安全的操作。

FString

与 FName 和 FText 不同,FString 是唯一允许操作的字符串类。字符串操作的可用方法有许多,如大小写转换、摘录子字符串和逆向。FStrings 可被搜集、修改、并与其他字符串进行对比。然而这些操作可能使 FString 的开销比不可变字符串类更大。

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 字符编码 documentation.