大家好,我是阿赵
之前在介绍了UE的log打印。打印输入的参数是字符串。这里来学习一下,UE里面字符串有哪些类型,还有一些常用的字符串处理方法。
一、 FName、FString、FText
1、 三种格式的介绍
在打印方法里面,输入的字符串的格式其实都是FString。但UE里面还有另外两种表达字符串的类型。
1. FName:最轻量化,表示名称用,不可更改。引擎里面的资源名称用。
2. FString:最接近std::string的类型,可以修改,提供各种方法,平时正常使用的字符串类型。
3. FText:带有很多功能,但不可更改,用于显示文本。还有UE自带的多语言收集功能。
FString是唯一可以修改的类型,平时写代码的时候,逻辑上使用的类型。
2、 三种格式的创建:
1. FName
FName nameVal = "this is a FName val";
FName nameVal = L"this is a FName val";
FName nameVal = TEXT("this is a FName val");
2. FString
FString stringVal = "this is a FString val";
FString stringVal = L"this is a FString val";
FString stringVal = TEXT("this is a FString val");
3. FText
FText textVal1 = FText();
FText textVal2 = FText::GetEmpty();
FText textVal3 = NSLOCTEXT("test", "FTextVal", "this is a FTest val");
第一个参数是namespace,代表着这个FText的用途
第二个参数是key,代表着这个FText的id
第三个参数是实际显示的字符串内容
创建字符串的时候,可以直接双引号包含字符串,也可以前面加L,或者用TEXT宏来包裹。如果直接用字符串,会存在中文编码问题,其他两种,会转Unicode。在UE里面,习惯都使用TEXT宏来包裹字符串。
3、 互相转换
1. FName转其他
FName转FString
FName nameVal = TEXT("this is a FName val");
FString stringVal = nameVal.ToString();
FName转FText
FName nameVal = TEXT("this is a FName val");
FText textVal = FText::FromName(nameVal);
2. FString转其他
FString转FName
FString stringVal = TEXT("this is a FString val");
FName nameVal = FName(*stringVal);
FString转FText
FString stringVal = TEXT("this is a FString val");
FText textVal = FText::FromString(stringVal);
3. FText转其他
FText转FString
FText textVal = NSLOCTEXT("test", "FTextVal", "this is a FTest val");
FString stringVal = textVal.ToString();
这时候打印stringVal,会显示"this is a FTest val"
FText转FName
没有直接转换方法,需要先转FString,再转FName
FText textVal = NSLOCTEXT("test", "FTextVal", "this is a FTest val");
FString stringVal = textVal.ToString();
FName nameVal = FName(*stringVal);
二、 FString操作
1、 对比
1. 等号
FString val1 = TEXT("val1");
FString val2 = TEXT("val2");
bool isEqual = (val1 == val2);
2. equal
FString val1 = TEXT("val1");
FString val2 = TEXT("val2");
bool isEqual = val1.Equals(val2);
//考虑大小写
FString val1 = TEXT("val1");
FString val2 = TEXT("val2");
bool isEqual = val1.Equals(val2, ESearchCase::CaseSensitive);
//忽略大小写
FString val1 = TEXT("val1");
FString val2 = TEXT("Val1");
bool isEqual = val1.Equals(val2, ESearchCase::IgnoreCase);
3. 比较大小
FString val1 = TEXT("123");
FString val2 = TEXT("456");
int compareResult = val1.Compare(val1);
大于:1
等于:0
小于:-1
2、 拼接
1.加号连接
FString val1 = TEXT("val1");
FString val2 = TEXT("val2");
FString val3 = val1 + val2;
2. Append连接
FString val1 = TEXT("val1");
FString val2 = TEXT("val2");
FString val3 = val1.Append(*val2);
3、 判空
FString stringVal = FString("");
stringVal.IsEmpty()
4、 清空
FString stringVal = FString(TEXT("abcde"));
stringVal.Empty();
5、 Trim
1.赋值:
FString stringVal1 = FString(TEXT("\t\rabcdefg\t\r "));
FString stringVal2 = stringVal1.TrimStartAndEnd();
//FString stringVal2 = stringVal1.TrimStart();
//FString stringVal2 = stringVal1.TrimEnd();
2.改变自身:
FString stringVal1 = FString(TEXT("\t\rabcdefg\t\r "));
stringVal1.TrimStartAndEndInline();
//stringVal1.TrimStartInline();
//stringVal1.TrimEndInline();
6、 取长度
FString stringVal = FString(TEXT("abcde"));
int len = stringVal.Len();
7、 是否包含
判断包含
FString stringVal1 = FString(TEXT("abcde"));
FString stringVal2 = FString(TEXT("bc"));
bool isContains = stringVal1.Contains(stringVal2);
查找子字符串位置
FString stringVal1 = FString(TEXT("abcde"));
FString stringVal2 = FString(TEXT("bc"));
int index = stringVal1.Find(stringVal2);
查找子字符串开头
FString stringVal1 = FString(TEXT("abcde"));
FString stringVal2 = FString(TEXT("abc"));
bool result = stringVal1.StartsWith(stringVal2);
查找子字符串结尾
FString stringVal1 = FString(TEXT("abcde"));
FString stringVal2 = FString(TEXT("de"));
bool result = stringVal1.EndsWith(stringVal2);
8、 格式化
1. FString::Printf
float floatVal = 1.5f;
int intVal = 4;
FString stringVal = TEXT("aaa");
FString newString = FString::Printf(TEXT("浮点:%f,整型:%d,字符串:%s"), floatVal, intVal, *stringVal);
2. FString::Format
按顺序
float floatVal = 1.5f;
int intVal = 4;
FString stringVal = TEXT("aaa");
TArray<FStringFormatArg> args;
args.Add(floatVal);
args.Add(intVal);
args.Add(*stringVal);
FString newString = FString::Format(TEXT("浮点:{0},整型:{1},字符串:{2}"),args);
按key
float floatVal = 1.5f;
int intVal = 4;
FString stringVal = TEXT("aaa");
FStringFormatNamedArguments nameArgs;
nameArgs.Add(TEXT("name1"), floatVal);
nameArgs.Add(TEXT("name2"), intVal);
nameArgs.Add(TEXT("name3"), *stringVal);
FString newString = FString::Format(TEXT("浮点:{name1},字符串:{name3},整型:{name2}"),nameArgs);
9、 截取
1.赋值
从左边开始截取
FString stringVal1 = FString(TEXT("abcdefg"));
FString stringVal2 = stringVal1.Left(4); //abcd
从左边排除后面截取
FString stringVal1 = FString(TEXT("abcdefg"));
FString stringVal2 = stringVal1.LeftChop(4);//abc
从右边截取
FString stringVal1 = FString(TEXT("abcdefg"));
FString stringVal2 = stringVal1.Right(4);//defg
从右边排除前面截取
FString stringVal1 = FString(TEXT("abcdefg"));
FString stringVal2 = stringVal1.RightChop(4);//efg
从中间指定位置截取
FString stringVal1 = FString(TEXT("abcdefg"));
FString stringVal2 = stringVal1.Mid(3,2);//de
2.改变自身
FString stringVal1 = FString(TEXT("abcdefg"));
stringVal1.LeftInline(4);
//stringVal1.LeftChopInline(4);
//stringVal1.RightInline(4);
//stringVal1.RightChopInline(4);
//stringVal1.MidInline(4,2);
10、 删除
从前面删除指定
FString stringVal1 = FString(TEXT("abcdefg"));
bool isSuccess = stringVal1.RemoveFromStart(TEXT("abc"));//defg
从后面删除指定
FString stringVal1 = FString(TEXT("abcdefg"));
bool isSuccess = stringVal1.RemoveFromEnd(TEXT("efg"));//abcd
删除指定长度
FString stringVal1 = FString(TEXT("abcdefg"));
stringVal1.RemoveAt(2,3);//abfg
11、 分割
FString stringVal1 = FString(TEXT("a,b,c,d,e,f,g"));
TArray<FString> stringArr;
stringVal1.ParseIntoArray(stringArr, TEXT(","), false);
for (int i = 0; i < stringArr.Num();i++)
{UE_LOG(LogTemp, Display, TEXT("%s"), *stringArr[i]);
}
12、 插入
FString stringVal1 = FString(TEXT("abcdefg"));
FString stringVal2 = FString(TEXT("123"));
stringVal1.InsertAt(2, *stringVal2);//ab123cdefg
13、 替换
1.赋值
FString stringVal1 = FString(TEXT("abacadaeaf"));
FString stringVal2 = stringVal1.Replace(TEXT("a"), TEXT("1"));//1b1c1d1e1f
2.改变自身
FString stringVal1 = FString(TEXT("abacadaeaf"));
stringVal1.ReplaceInline(TEXT("a"), TEXT("1"));
14、 大小写转换
1. 赋值
转大写
FString stringVal1 = FString(TEXT("abcde"));
FString stringVal2 = stringVal1.ToUpper();//ABCDE
转小写
FString stringVal1 = FString(TEXT("ABCDE"));
FString stringVal2 = stringVal1.ToLower();//abcde
2. 改变自身
转大写
FString stringVal1 = FString(TEXT("abcde"));
stringVal1.ToUpperInline();
转小写
FString stringVal1 = FString(TEXT("ABCDE"));
stringVal1.ToLowerInline();
15、 各种格式和FString转换
这里我写了一个工具类,里面包含各种转换方法:
MyStringTool.h
#pragma once#include "CoreMinimal.h"/*** */
class UECPPTEST_API MyStringTool
{
public:MyStringTool();~MyStringTool();/// <summary>/// 布尔转FString/// </summary>/// <param name="val">布尔值</param>/// <returns></returns>static FString BoolToFString(bool val);/// <summary>/// FString转布尔/// </summary>/// <param name="str"></param>/// <returns></returns>static bool FStringToBool(FString str);/// <summary>/// 整型转FString/// </summary>/// <param name="val">整型值</param>/// <returns></returns>static FString IntToFString(int val);/// <summary>/// FString转int32,转换失败时返回0/// </summary>/// <param name="str">FString</param>/// <returns></returns>static int32 FStringToInt32(FString str);/// <summary>/// FString转int64,转换失败时返回0/// </summary>/// <param name="str">FString</param>/// <returns></returns>static int64 FStringToInt64(FString str);/// <summary>/// Double转FString,指定小数长度,如果本身小数位大于指定长度,会保留原小数位/// </summary>/// <param name="val">double值</param>/// <param name="len">长度</param>/// <returns></returns>static FString DoubleToFString(double val, int len);/// <summary>/// Double转FString/// </summary>/// <param name="val">double值</param>/// <returns></returns>static FString DoubleToFString(double val);/// <summary>/// FString转浮点,转换失败时返回0/// </summary>/// <param name="val">FString</param>/// <returns></returns>static float FStringToFloat(FString val);/// <summary>/// FString转double,转换失败时返回0/// </summary>/// <param name="val">FString</param>/// <returns></returns>static double FStringToDouble(FString val);/// <summary>/// FString转std::string/// </summary>/// <param name="val">FString</param>/// <returns></returns>static std::string FStringToSTDString(FString val);/// <summary>/// std::string转FString/// </summary>/// <param name="val">std::string</param>/// <returns></returns>static FString STDStringToFString(std::string val);
};
MyStringTool.cpp
#include "MyStringTool.h"
#include "string"MyStringTool::MyStringTool()
{
}MyStringTool::~MyStringTool()
{
}
/// <summary>
/// 布尔转FString
/// </summary>
/// <param name="val">布尔值</param>
/// <returns></returns>
FString MyStringTool::BoolToFString(bool val)
{return val ? TEXT("true") : TEXT("false");
}
/// <summary>
/// FString转布尔
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
bool MyStringTool::FStringToBool(FString str)
{return str.ToBool();
}
/// <summary>
/// int32转FString
/// </summary>
/// <param name="val">int32数值</param>
/// <returns></returns>
FString MyStringTool::IntToFString(int32 val)
{return FString::FromInt(val);
}
/// <summary>
/// int64转FString
/// </summary>
/// <param name="val">int64数值</param>
/// <returns></returns>
FString MyStringTool::Int64ToFString(int64 val)
{return FString::Printf(TEXT("%lld"), val);
}/// <summary>
/// FString转int32,转换失败时返回0
/// </summary>
/// <param name="str">FString</param>
/// <returns></returns>
int32 MyStringTool::FStringToInt32(FString str)
{return FCString::Atoi(*str);
}
/// <summary>
/// FString转int64,转换失败时返回0
/// </summary>
/// <param name="str">FString</param>
/// <returns></returns>
int64 MyStringTool::FStringToInt64(FString str)
{return FCString::Atoi64(*str);
}/// <summary>
/// Double转FString,指定小数长度,如果本身小数位大于指定长度,会保留原小数位
/// </summary>
/// <param name="val">double值</param>
/// <param name="len">长度</param>
/// <returns></returns>
FString MyStringTool::DoubleToFString(double val,int len)
{return FString::SanitizeFloat(val,len);
}
/// <summary>
/// Double转FString
/// </summary>
/// <param name="val">double值</param>
/// <returns></returns>
FString MyStringTool::DoubleToFString(double val)
{return FString::SanitizeFloat(val);
}/// <summary>
/// FString转浮点,转换失败时返回0
/// </summary>
/// <param name="val">FString</param>
/// <returns></returns>
float MyStringTool::FStringToFloat(FString val)
{return FCString::Atof(*val);
}/// <summary>
/// FString转double,转换失败时返回0
/// </summary>
/// <param name="val">FString</param>
/// <returns></returns>
double MyStringTool::FStringToDouble(FString val)
{return FCString::Atod(*val);
}/// <summary>
/// FString转std::string
/// </summary>
/// <param name="val">FString</param>
/// <returns></returns>
std::string MyStringTool::FStringToSTDString(FString val)
{return TCHAR_TO_UTF8(*val);
}/// <summary>
/// std::string转FString
/// </summary>
/// <param name="val">std::string</param>
/// <returns></returns>
FString MyStringTool::STDStringToFString(std::string val)
{return FString(val.c_str());
}