繁体   English   中英

使用特殊字符将 std::string 转换为 FString (TCHAR / wstring)

[英]Convert std::string to FString (TCHAR / wstring) with special characters

使用虚幻引擎 4,我想从机器加载一个文件,其中包含像''这样的字符。

所有转换尝试都会导致最终的FString包含? 在他们的位置上,或者根本没有角色。

FString是 UE4 的内部字符串类,它使用TCHAR ( wchar_t ),它使用 UTF-16 编码。

即使绝望地尝试使用它也失败了:

std::replace(str.begin(), str.end(), L'“', L'\"');

什么都没有发生。

如何在std::stringFString之间正确转换?

将您的std::string转换为std::wstring因为它也在wchar_t模板化,并尝试用它初始化您的FString
如果您不知道如何转换为wstring请查看此主题: c++ can't convert string to wstring
然后你可以做这样的事情:
FString str = FString(your_wstring.c_str()); 或者
FString str(your_wstring.c_str());

您也可以尝试从文件中直接读取数据到wstring甚至FString因为 UE4 有自己的类来管理文件,例如FFileHelperhttp : FFileHelper /index.html ,我真的会推荐你最后一个选项:-)

您可以将 std::string 转换为 FString ,然后像这样记录。

std::string someString = "Hello!";

FString unrealString(someString .c_str());

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

比你可以做的 FString 替换功能就可以了。 https://docs.unrealengine.com/en-US/API/Runtime/Core/Containers/FString/Replace/index.html

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM