如何区分中英文字符
在ue4 FString字符串中如何判断每一个字符是中文还是英文?思路是将FString中每一个TCHAR转成UTF8,根据UTF8的长度判断是中文还是英文,中文的长度是3,英文的长度是1。主要用到了UE4封装的FTCHARToUTF8。具体代码如下:
FString somestring;
int cnt = 0;
while(cnt <= somestring.Len())
{
FTCHARToUTF8 sometchar_utf8(&somestring[cnt],1);
if(sometchar_utf8.Length() == 3)
else
cnt++;
}
知识要点: 1、UTF8编码方式 2、UE4源码StringConv.h ,截取相关性大的一些源码:
typedef TStringConversion<FTCHARToUTF8_Convert> FTCHARToUTF8;
template<typename Converter, int32 DefaultConversionSize = DEFAULT_STRING_CONVERSION_SIZE>
class TStringConversion : private Converter, private TInlineAllocator<DefaultConversionSize>::template ForElementType<typename Converter::ToType>
{
typedef typename TInlineAllocator<DefaultConversionSize>::template ForElementType<typename Converter::ToType> AllocatorType;
typedef typename Converter::FromType FromType;
typedef typename Converter::ToType ToType;
void Init(const FromType* Source, int32 SourceLen, ENullTerminatedString::Type NullTerminated)
{
StringLength = Converter::ConvertedLength(Source, SourceLen);
int32 BufferSize = StringLength + NullTerminated;
AllocatorType::ResizeAllocation(0, BufferSize, sizeof(ToType));
Ptr = (ToType*)AllocatorType::GetAllocation();
Converter::Convert(Ptr, BufferSize, Source, SourceLen + NullTerminated);
}
public:
TStringConversion(const FromType* Source, int32 SourceLen)
{
if (Source)
{
ENullTerminatedString::Type NullTerminated = ENullTerminatedString::No;
if (SourceLen > 0 && Source[SourceLen-1] == 0)
{
NullTerminated = ENullTerminatedString::Yes;
SourceLen -= 1;
}
Init(Source, SourceLen, NullTerminated);
}
else
{
Ptr = nullptr;
StringLength = 0;
}
}
FORCEINLINE int32 Length() const
{
return StringLength;
}
private:
TStringConversion(const TStringConversion&) = delete;
TStringConversion& operator=(const TStringConversion&) = delete;
ToType* Ptr;
int32 StringLength;
};
|