Problem finding Key within TMap

Hi all,

I have a TMap which I am constructing using a FString Key and a Struct Value (this struct is just 2 FStrings) from a XML parsed file.

I seem to be parsing the XML file correctly and populating the TMap with the data, but I am having problems finding the ‘key’ with the TMap after construction. Here is my code so far…



        const FXmlFile file(L"D:/file.xml");
	const FXmlNode* SceneNode = file.GetRootNode();
	FString scenename = SceneNode->GetTag();

	// Get all child nodes
	TArray<FXmlNode*> nodes = SceneNode->GetChildrenNodes();
	TMap<FString, FLanguageData*> LanguageLocalised;

	// Create TMap
	for (int i = 0; i < nodes.Num(); ++i)
	{
		if (nodes*->GetTag() == "textid")
		{
			FString english = nodes[i + 1]->GetContent();
			FString french = nodes[i + 2]->GetContent();

			// Create our Language Struct and add it to the Map file
			FLanguageData* value = new FLanguageData();
			value->English = english;
			value->French = french;

			FString key = nodes*->GetContent();
			LanguageLocalised.Add(key, value);
		}
	}
	
	// Contains
	bool contains = LanguageLocalised.Contains("key1");
	if (contains == true)
	{
		UE_LOG(LogTemp, Warning, TEXT("TRUE"));
	}
	else
	{
		UE_LOG(LogTemp, Warning, TEXT("FALSE"));
	}

	// Loop through Map
	for (auto& KVP : LanguageLocalised)
	{
		FString Key = KVP.Key;
		FLanguageData* ld = KVP.Value;

		UE_LOG(LogTemp, Warning, TEXT("Search for keys: %s"), *Key);
		UE_LOG(LogTemp, Warning, TEXT("Search for value: %s"), *ld->English);
	}


My basic aim is to output the data from the corresponding key value, “key1”. You can see from my OutputLog (see image below) that the contains if state is returning FALSE. You can also see from this log that ‘key1’ is in the TMap (see Loop through Map in code snippet).

If I hardcode an additional entry into the TMap I can find it with a ‘contains’ just fine, so I’m guessing it’s related to my ‘FString key = nodes*->GetContent();’.

Any help would be greatly received!!!

Best regards,
Matt.

An update:

As I’m passing in a ‘hardcoded’ string into TMap.Contains(“key1”) or TMap.Contains(TEXT(“key1”)), and this is being compared against the original stored key string which came from a pointer: nodes*->GetContent(); (XmlParser), would this be why the comparison is failing?

My knowledge of c++ is one of learning, so any help would be greatly received :slight_smile:

Kind regards,
Matt.

Try this:

bool contains = LanguageLocalised.Contains(TEXT(“key1”));

FStrings are two-byte character strings, whereas C++ literals are one-byte. The TEXT macro converts it to two bytes.

> FStrings are two-byte character strings

Not always but most often. Depends on the platform. A FString is either a array of wchar_t or char16. wchar_t can be anything from 8bit (dont know any modern platform that still has this but its still supported), 16bit (very common) or 32bit (there is at least one platform that I know which has this). The TEXT macro switches between wchar_t and char16 (L and u prefix for strings) for the TChar type which is the type of a character of a FString (or FText).