Page MenuHomeFeedback Tracker

string.Replace truncates long text
Awaiting internal Testing, NormalPublic

Description

If the text where a replacement is taking place is long (threshold seems to be around 2500 characters), the text gets truncated during the replace operation. This only seems to happen if the replacement text is shorter than the search text.

Details

Severity
None
Resolution
Open
Reproducibility
Always
Operating System
Windows 10 x64
Category
Modding
Steps To Reproduce

Example:

string test = "Imagine a long string here, for example around 2500 characters [...] [...]";
test.Replace("example", "x");   // `test` is truncated after the replace operation
Additional Information

Workaround:

int SafeReplace(inout string content, string search, string replace)
{
	int count;
	int searchLen = search.Length();
	int replaceLen = replace.Length();
	int index = content.IndexOf(search);
	while (index > -1)
	{
		content = content.Substring(0, index) + replace + content.Substring(index + searchLen, content.Length() - index - searchLen);
		count++;
		index = content.IndexOfFrom(index + replaceLen, search);
	}

	return count;
}

Event Timeline

lava76 created this task.Nov 29 2023, 5:46 PM
lava76 updated the task description. (Show Details)Nov 29 2023, 6:20 PM
Geez changed the task status from New to Awaiting internal Testing.Nov 30 2023, 12:29 PM