UE-c++ FString

一. 类介绍

与 FName 和 FText 不同,FString 可以与搜索、修改并且与其他字符串比较。不过,这些操作会导致 FString 的开销比不可变字符串类更大。这是因为 FString 对象保存自己的字符数组,而 FName 和 FText 对象保存共享字符数组的指针,并且可以完全根据索引值建立相等性。

二. 创建FString

类似 std::string 。使用宏 TEXT() 可以新建一个。

	//Mark with the TEXT macro
	FString TempString = FString(TEXT("HEllo"));

三. FString和其他类型转换

1. FString to Others

	//FString to std::string
	std::string StdString_ANSI = TCHAR_TO_ANSI(*TempString);
	std::string StdString_UTF8 = TCHAR_TO_UTF8(*TempString);

	//FString to bool
	bool TempBool = TempString.ToBool();

	//FString to int
	int32 TempInt = FCString::Atoi(TEXT("100"));

	//FString to double
	double TempDoule = FCString::Atod(TEXT("1.111"));

	//FString to float
	float TempFloat = FCString::Atof(TEXT("1.11f"));

	/* FString to FName
	 * Unreliable,FName is case insensitive
	 * conversion is lossy
	 */
	FName TempName = FName(*TempString);

	/* FString to FText
	 * This works in some cases, but note that FString
	 * content does not benefit from FText's auto localization
	 */
	FText TempText=FText::FromString(*TempString);

	//Other conversions write their own conversion functions

2. Others to FString

	//std::string to FString
	TempString=StdString_UTF8.c_str();

	//bool to FString
	TempString=true?FString(TEXT("true")):FString(TEXT("false"));

	//int to FString
	TempString=FString::FromInt(123);

	//float/double to FString
	TempString=FString::SanitizeFloat(1.23f);

	//FName to FString
	TempString=TempName.ToString();
	
	/* FText to FString
	 * Unreliable and potentially lossy in some
	 * language conversions
	 */
	TempString=TempText.ToString();

	//Write your own conversion function for others
	TempString=true?TEXT("true"):TEXT("false");

	//Or use the class's built in ToString() function
	TempString=(new FVector())->ToString();

	/* Or use the FString::Printf function, the same
	 * format as C's Printf() function
	 */
	TempString=FString::Printf(TEXT("ParamInt:%-10.2f"),112.1231379f);

	/* Or use the FString::Format() function
	 * which is similar to Python/Js string
	 * interpolation, see concatenation
	 */

四. FString函数操作

1. 属性

	const FString TempAttribute =TEXT("HelloWorld");
	//String size
	PRINT(FString::FromInt(TempAttribute.Len()));//10
	//String iteration
	for (auto i=TempAttribute.begin();i!=TempAttribute.end();++i) {
		PRINT(FString::Printf(TEXT("%c"),*i));
	}
	//TempAttribute[0];

	//GetCharArray() Get an array of characters
	PRINT(FString::Printf(TEXT("%c"),TempAttribute.GetCharArray()[0]));//H

2. 判断

	FString TempJudge=TEXT("HelloWorld");
	//Is the String empty?
	PRINT(TempJudge.IsEmpty()?FString(TEXT("true")):FString(TEXT("false")));//false
	//Is the String numeric?
	PRINT(TempJudge.IsNumeric()?FString(TEXT("true")):FString(TEXT("false")));//false
	//Is the index valid?
	PRINT(TempJudge.IsValidIndex(999)?FString(TEXT("true")):FString(TEXT("false")));//false

	TempJudge=TEXT("ab");
	//Whether the String begins with special string?
	PRINT(TempJudge.StartsWith(TEXT("a"))?FString(TEXT("true")):FString(TEXT("false")));//true
	//Whether the String ends with special string?
	PRINT(TempJudge.EndsWith(TEXT("b"))?FString(TEXT("true")):FString(TEXT("false")));//true

3. 比较

	const FString TempCompare1=TEXT("123");
	const FString TempCompare2=TEXT("125");
	/* Equals() Compare two FStrings and return true if they are equal
	 * ESearchCase::IgnoreCase Ignores case
	 * ESearchCase::CaseSensitive is case sensitive
	 */
	bool bResult;
	bResult=(TempCompare1==TempCompare2);
	bResult=(TempCompare1.Equals(TempCompare2));

	/* Compare() Compare two FStrings by ASCII size,
	 * greater than 1, equal to 0, less than -1
	 */
	PRINT(FString::FromInt(TempCompare1.Compare(TempCompare2)));//-1
	PRINT(FString::FromInt(TempCompare2.Compare(TempCompare2)));//0
	PRINT(FString::FromInt(TempCompare2.Compare(TempCompare1)));//1

4. 拼接

	FString TempJoin1=TEXT("123");
	FString TempJoin2=TEXT("125");
	
	//Use the operator directly
	TempJoin1=TempJoin1+TempJoin2;
	TempJoin1+=TempJoin2;

	//Use the FString::Printf() function, consistent with C/C++ Printf() format
	TempJoin1=FString::Printf(TEXT("%02d:%02d"),20,5);

	/* Use the FString::Format() function in a format
	 * similar to Python/Js string interpolation
	 */
	//Sequential parameters (parameters are numbered sequentially)
	FStringFormatOrderedArguments OrderedArgs;
	OrderedArgs.Add(FStringFormatArg(FString::SanitizeFloat(100.0f,1)));
	OrderedArgs.Add(FStringFormatArg(100.0f));//Note that the engine defaults to 6 decimal places
	TempString=FString::Format(TEXT("Health:{0}/{1}"),OrderedArgs);
	PRINT(TempString);
	//Named parameters (parameters are given by parameter name)
	FStringFormatNamedArguments NamedArgs;
	NamedArgs.Add(TEXT("CurrentHealth"),100.0f);
	NamedArgs.Add(TEXT("MaxHelath"),100.0f);
	TempString=FString::Format(TEXT("Health:{CurrentHealth}/{MaxHealth}"),NamedArgs);
	PRINT(TempString);

5. 分割

	const FString TempSplit=TEXT("HelloWorld");
	//Separates the string into two strings
	FString*a=new FString(),*b=new FString();
	TempSplit.Split(TEXT("o"),a,b);
	PRINT(*a);//Hell
	PRINT(*b);//World
	//Splits the string into n strings
	TArray<FString>StringArray;
	TempSplit.ParseIntoArray(StringArray,TEXT("o"));
	PRINT(StringArray[0]);//Hell
	PRINT(StringArray[1]);//W
	PRINT(StringArray[2]);//rld

6. 变换

	const FString TempChange =TEXT("HelloWorld");
	//Reverse() As its name suggests
	PRINT(TempChange.Reverse());//dlroWolleH
	//Replace() As its name suggests
	PRINT(TempChange.Replace(TEXT("o"),TEXT("fff")));//HellfffWffforld
	//ToUpper() Convert to Uppercase
	PRINT(TempChange.ToUpper());//HELLOWORLD
	//ToLower() Convert to Lowercase
	PRINT(TempChange.ToLower());//helloworld
	//Trim whitespace
	const FString TempSpace =TEXT("  \"ab cd\"  ");
	PRINT(TempSpace);
	PRINT(TempSpace.TrimStart());//"ab cd  "
	PRINT(TempSpace.TrimEnd());//"  ab cd"
	PRINT(TempSpace.TrimStartAndEnd());//"ab cd"
	/* TrimQuotes() Note that the effect is thus head and tail,
	 * and is not effective in the middle
	 */ 
	PRINT(TempSpace.TrimStartAndEnd().TrimQuotes());//ab cd

7. 搜索

	FString TempSearch=TEXT("HelloWorld");
	const  FString TempSub=TEXT("o");
	/* Contains() returns true when found
	 * Find() returns the first index of the first substring
	 * Find() can also select the index to start the search
	 * ESearchDir::FromStart Search from the beginning
	 * ESearchDir::FromEnd Search from the end
	 */
	bResult=TempSearch.Contains(TempSub,ESearchCase::CaseSensitive,ESearchDir::FromStart);
	int32 Index=TempSearch.Find(TempSub,ESearchCase::CaseSensitive,ESearchDir::FromStart,1);

	TempSearch=TEXT("HelloWorld");
	//Note that the index starts at 0
	//Left() Cut num characters from the left
	PRINT(TempSearch.Left(3));//Hel
	//Middle() Cut start-end characters from the middle
	PRINT(TempSearch.Mid(1,5));//elloW
	//Right() Cut num characters from the right
	PRINT(TempSearch.Right(3));//rld
	
	//Find the first specified character
	TempSearch.FindChar('o',Index);
	TempSearch.FindLastChar('o',Index);
	//You can also Find by predicate
	//TempSearch.FindLastCharByPredicate([](){});

	const FString TempOther=TEXT("ABCDE");
	/* Pad() If the total length is less than Num,
	 * add spaces to the left/right until the length equals Num
	 */
	PRINT(TempOther.LeftPad(10));//     ABCDE
	PRINT(TempOther.RightPad(10));//ABCDE     

	//Similar to Left()/Right(),except that the index starts at 0
	PRINT(TempOther.LeftChop(2));//ABC
	PRINT(TempOther.RightChop(2));//CDE

8. 其他自行练习

五. 其他

宏定义真的很好用

	#define PRINT(String) {if (GEngine){GEngine->AddOnScreenDebugMessage(-1,10.0f,FColor::Red,*(String));}}
  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值