# Character encoding problem with FString::Contains

**URL:** <https://forums.unrealengine.com/t/character-encoding-problem-with-fstring-contains/1184638>\
**Category:** Programming & Scripting\
**Tags:** unreal-engine, CPP, question\
**Created:** [May 31, 2023, 2:32pm UTC](https://forums.unrealengine.com/t/character-encoding-problem-with-fstring-contains/1184638 "2023-05-31T14:32:10Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![F1nansist](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@F1nansist](https://forums.unrealengine.com/u/F1nansist)\
**Post date:** [May 31, 2023, 2:32pm UTC](https://forums.unrealengine.com/t/character-encoding-problem-with-fstring-contains/1184638/1 "2023-05-31T14:32:10Z")

</div>

Hello  
I’m making a search field on UMG. I have string array and simple string as input and string array as output. I just need to find all matches in input string array by simple string and put result as output.

The problem is when i try to make lower case from russian chars. Unreal Engine doesnt want to make lower case from russian language. I find in documentation that UE4 handles only ANSI. Honestly i dont wanna make any iteration stuff to make by myself big chars into small one.

There is any solution in this situation?

ref:

> **[Character Encoding](https://docs.unrealengine.com/4.27/en-US/ProgrammingAndScripting/ProgrammingWithCPP/UnrealArchitecture/StringHandling/CharacterEncoding/)**
>
> Overview of character encodings used in Unreal Engine.

---

<div class="post-metadata">

**Author:** ![3dRaven](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/3draven/32/906552_2.png) [@3dRaven](https://forums.unrealengine.com/u/3dRaven)\
**Post date:** [May 31, 2023, 3:43pm UTC](https://forums.unrealengine.com/t/character-encoding-problem-with-fstring-contains/1184638/2 "2023-05-31T15:43:12Z")

</div>

the base encoding unforunately won’t work with the russian language.

```auto
FString text = "test";
FString s(TCHAR_TO_UTF8(*passedText));
s = s.ToLower();	

```

Breaks all non-latin characters .

As in the documentation you would need a converter from ANSI to ISO/IEC 8859-5 and back.

> ## ToUpper() and ToLower() Non-Trivial in Unicode
> 
> UE4 currently only handles ANSI (ASCII | code page 1252 | | Western European).
> 
> The least worst method to do this for all languages seems to be mentioned here [en.wikipedia.org/wiki/ISO/IEC\_8859](http://en.wikipedia.org/wiki/ISO/IEC_8859)
> 
> - ISO/IEC 8859-1 for English, French, German, Italian, Portuguese, and both Spanishes
> - ISO/IEC 8859-2 for Polish, Czech, and Hungarian
> - ISO/IEC 8859-5 for Russian
> 
> The mappings from ftp.unicode.org/Public/MAPPINGS/ISO8859/ contain the conversion rules for the above mentioned languages. ‘CAPITAL LETTER’ and ‘SMALL LETTER’ info would be cross referenced with the appropriate unicode character to get the desired result.

---

<div class="post-metadata">

**Author:** ![F1nansist](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@F1nansist](https://forums.unrealengine.com/u/F1nansist)\
**Post date:** [June 1, 2023, 11:08am UTC](https://forums.unrealengine.com/t/character-encoding-problem-with-fstring-contains/1184638/3 "2023-06-01T11:08:18Z")

</div>

It sounds good but im not sure that my c++ experience is enough to get through this problem.

By the way, i solve this problem inside blueprints with replacement russians chars in array.

Thank u for answer

---

<div class="post-metadata">

**Author:** ![3dRaven](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/3draven/32/906552_2.png) [@3dRaven](https://forums.unrealengine.com/u/3dRaven)\
**Post date:** [June 1, 2023, 1:22pm UTC](https://forums.unrealengine.com/t/character-encoding-problem-with-fstring-contains/1184638/4 "2023-06-01T13:22:43Z")

</div>

@F1nansist I got an international version working!

Digging through the various files and documentations I stumbled upon FTextTransformer

> **[FTextTransformer](https://docs.unrealengine.com/5.2/en-US/API/Runtime/Core/Internationalization/FTextTransformer/)**
>
> Utility for performing low-level localized transforms.

You just need add

```auto
#include "Internationalization/TextTransformer.h" 

```

Then you can try

```auto
	FString s(TCHAR_TO_UTF16(*passedText));					
	FString passedTextLow = FTextTransformer::ToLower(s);
	FString passedTextUP = FTextTransformer::ToUpper(s);

```

Where passedText is a FString

Seems to work with many other languages. I tested it with Polish and it does upper case and lower case well, also did a quick test with some Russian letters and seems to handle them well 🙂

 ![test1](https://d3kjluh73b9h9o.cloudfront.net/original/4X/6/a/b/6ab12ba651ad3356a6644d3bed1ecbe24a23e1f8.png)  
 ![test2](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/7/2/272e423949e16a6561afcaa0b70b7d0f709dd75c.png)

@EpicChris I was wondering if someone from the staff could maybe incorporate a comparison like this natively for unicode test of the contains function for strings?

Native uppercase & lowercase for FText should use UTF16 with FTextTransformer or at least have an option for unicode compliant conversion? (maybe a bool)

---

<div class="post-metadata">

**Author:** ![3dRaven](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/3draven/32/906552_2.png) [@3dRaven](https://forums.unrealengine.com/u/3dRaven)\
**Post date:** [June 1, 2023, 2:12pm UTC](https://forums.unrealengine.com/t/character-encoding-problem-with-fstring-contains/1184638/5 "2023-06-01T14:12:47Z")

</div>

.h

```auto
	UFUNCTION(BlueprintCallable, BlueprintPure)
		static FString ToLower(const FString& passedText);

	UFUNCTION(BlueprintCallable, BlueprintPure)
	static FString ToUpper(const FString& passedText);

```

.cpp

```auto
#include "Internationalization/TextTransformer.h" 

```

```auto
FString UTextExtraFunctionLibrary::ToLower(const FString& passedText) {	
	FString s(TCHAR_TO_UTF16(*passedText));					
	FString passedTextLow = FTextTransformer::ToLower(s);	
	return passedTextLow;
}

FString UTextExtraFunctionLibrary::ToUpper(const FString& passedText) {
	FString s(TCHAR_TO_UTF16(*passedText));	
	FString passedTextUP = FTextTransformer::ToUpper(s);	
	return passedTextUP;
}

```

---

<div class="post-metadata">

**Author:** ![F1nansist](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@F1nansist](https://forums.unrealengine.com/u/F1nansist)\
**Post date:** [June 1, 2023, 2:12pm UTC](https://forums.unrealengine.com/t/character-encoding-problem-with-fstring-contains/1184638/6 "2023-06-01T14:12:51Z")

</div>

Yeah, works on UE4. Thank u a lot.

---

<div class="post-metadata">

**Author:** ![3dRaven](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/3draven/32/906552_2.png) [@3dRaven](https://forums.unrealengine.com/u/3dRaven)\
**Post date:** [June 1, 2023, 2:35pm UTC](https://forums.unrealengine.com/t/character-encoding-problem-with-fstring-contains/1184638/7 "2023-06-01T14:35:05Z")

</div>

@F1nansist case sensitive version of find and contains

```auto
bool UTextExtraFunctionLibrary::ContainsCS(const TArray<FString> fullArray, const FString& passedText) {

	FString s = TCHAR_TO_UTF16(*passedText);
	
	for (int i = 0; i < fullArray.Num(); i++) {
		FString e =TCHAR_TO_UTF16(*fullArray[i]);
		if (e.Compare(s, ESearchCase::CaseSensitive) == 0) {
			return true;			
		}
	}
	return false;		
}

int32 UTextExtraFunctionLibrary::FindCS(const TArray<FString> fullArray, const FString& passedText) {

	FString s = TCHAR_TO_UTF16(*passedText);

	for (int i = 0; i < fullArray.Num(); i++) {
		FString e = TCHAR_TO_UTF16(*fullArray[i]);
		if (e.Compare(s,ESearchCase::CaseSensitive) == 0) {
			return i;

		}
	}
	return -1;
}

```

 ![findCS](https://d3kjluh73b9h9o.cloudfront.net/original/4X/a/d/9/ad9d64a1218811d82e51650d636a9db41c038e87.png)

The find and contains could probably be combined into 1 node you’d just have to return a tuple\<bool,int32\>. (if I remember right it’s TTuple in UE)

---

<div class="post-metadata">

**Author:** ![F1nansist](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@F1nansist](https://forums.unrealengine.com/u/F1nansist)\
**Post date:** [June 1, 2023, 2:40pm UTC](https://forums.unrealengine.com/t/character-encoding-problem-with-fstring-contains/1184638/8 "2023-06-01T14:40:23Z")

</div>

By the way, this is how looks my search in string array by input string

```auto
#include "Internationalization/TextTransformer.h"
#include "Algo/Copy.h"

void UFindUserPluginBPLibrary::CPPFindMatchesBySubstring(TArray<FString> InArray, FString TargetWord, bool IgnoreCase, TArray<FString>& FoundMatches)
{
	if (IgnoreCase)
		TargetWord = FTextTransformer::ToLower(TargetWord);

	Algo::CopyIf(InArray, FoundMatches, [&](const FString str) {
		FString temp(TCHAR_TO_UTF16(*str));
		if (IgnoreCase)
			temp = FTextTransformer::ToLower(temp);
		return temp.Find(TargetWord, ESearchCase::IgnoreCase) != INDEX_NONE;
	});
}

```

---

<div class="post-metadata">

**Author:** ![3dRaven](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/3draven/32/906552_2.png) [@3dRaven](https://forums.unrealengine.com/u/3dRaven)\
**Post date:** [June 1, 2023, 2:55pm UTC](https://forums.unrealengine.com/t/character-encoding-problem-with-fstring-contains/1184638/9 "2023-06-01T14:55:04Z")

</div>

@F1nansist  
You have a bug in your code try :

```auto

void UFindUserPluginBPLibrary::CPPFindMatchesBySubstring(TArray<FString> InArray, FString TargetWord, bool IgnoreCase, TArray<FString>& FoundMatches)
{
	if (IgnoreCase)
		TargetWord = FTextTransformer::ToLower(TargetWord);

	Algo::CopyIf(InArray, FoundMatches, [&](const FString str) {
		FString temp(TCHAR_TO_UTF16(*str));
		if (IgnoreCase) {
			temp = FTextTransformer::ToLower(temp);
			return temp.Find(TargetWord, ESearchCase::IgnoreCase) != INDEX_NONE;
		}
		else {
			return temp.Find(TargetWord, ESearchCase::CaseSensitive) != INDEX_NONE;
		}
		});
}

```

---

<div class="post-metadata">

**Author:** ![F1nansist](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@F1nansist](https://forums.unrealengine.com/u/F1nansist)\
**Post date:** [June 2, 2023, 8:37am UTC](https://forums.unrealengine.com/t/character-encoding-problem-with-fstring-contains/1184638/10 "2023-06-02T08:37:21Z")

</div>

@3dRaven

And also i forget to convert TargetWord to UTF-16)

Thank u a lot!

---

<div class="post-metadata">

**Author:** ![system](https://d3kjluh73b9h9o.cloudfront.net/original/4X/7/1/3/713c9d3f58553f0de89543d76a8a3a2779dc9fa4.png) [@system](https://forums.unrealengine.com/u/system)\
**Post date:** [July 2, 2023, 8:38am UTC](https://forums.unrealengine.com/t/character-encoding-problem-with-fstring-contains/1184638/11 "2023-07-02T08:38:07Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
