String is malformed

I have defined an FString in my C+±Class, but the first few characters are malformed:

“testtesttesttesttesttesttestesttest”

becomes:

320276-screenshot-2020-10-27-153040.jpg

I’ve tried:

FString("…")
TEXT("…")
FString(TEXT("…"))
Ensured that the file is saved in UTF-8

Can you help me?

Actually the variable is used as the URL for HTTP-Requests. I’ve just printed it out using BP because only there you could see that just the first few characters are broken. The invalid-url error thrown by the HTTP-Request just prints out the wrong characters.

I found a workaround by defining the default value as a string literal inside the URL-Creation function and using the variable as an optional overwrite like this:

FString BaseURL;
if(MyClass.Server == "")
    BaseURL = TEXT("https://somedomain.tld/something");
else
    BaseURL = MyClass.Server;

I don’t know exactly why, but this works.

Are you simply trying to have a variable string printed to the screen?

FString my_string = "testestetstestsetst";

this should work if you only want to print your string:

	if (GEngine)
	{
		GEngine->AddOnScreenDebugMessage(-1, 15.0f, FColor::Yellow, my_string);
	}

this is what you should do if you want to use the string in a formatted text:

	if (GEngine)
	{
		GEngine->AddOnScreenDebugMessage(-1, 15.0f, FColor::Yellow, FString::Printf(TEXT("Server: %s"),*my_string));
	}

Just solved this problem on my end. Surprisingly i was also doing http requests, so i guess we had the similar setup:

//Backend.h:

UCLASS()
class TSARSTVA_API UBackend : public UObject
{
	GENERATED_BODY()
	
public:	
	UBackend();

	FString RequestAddress;
	FString RequestPort;

	void MakeRequest();
};

// mygamemode.h
UCLASS()
class TSARSTVA_API AMyGameMode : public AGameMode
{
	GENERATED_BODY()

public:
	AMyGameMode ();

	UBackend* Backend = nullptr;

	void BeginPlay() override;
};

//mygamemode.cpp

AMyGameMode::AMyGameMode()
{
	Backend = CreateDefaultSubobject<UBackend>(TEXT("Backend"));
}

void AMyGameMode::BeginPlay()
{
	Super::BeginPlay();

	FTimerHandle _;
	GetWorld()->GetTimerManager().SetTimer(_, [this](){Backend->MakeRequest()}, 3, true);
}

and the reason was that garbage collector was killing my UBackend right after creation, corrupting a few symbols in the strings, but without a crash. So you just have to mark your UBackend* as UPROPERTY() to avoid situation where uobject have no tracked references in the ue system