Unity freezes momentarily with large string operation

I have this ginormous string that is downloaded that contains data. I then use the string.split operation to break it down into usable parts.

When I run this operation unity freezes momentarily. Id like to run this operation ASYNC to avoid the freeze. But even as a coroutine I still get the same problem.

Any ideas on how to avoid a freeze on a process intensive operation?

Im using C#.


working code

	private IEnumerator SplitStringAsync(string str, string delimitor)
		string workingStr = str;
		int currentIndex = 0;
		int foundDelimitorIndex = workingStr.IndexOf(delimitor,currentIndex);
		while(foundDelimitorIndex != null)
			string newString = workingStr.Substring(currentIndex,foundDelimitorIndex);
			workingStr = workingStr.Replace(newString, "");
			foundDelimitorIndex = workingStr.IndexOf(delimitor,currentIndex + 1);
			newString = newString.Replace(delimitor, "");
			Debug.Log (newString);
			yield return null;

Coroutines are not threads. You can, however, use threads (as long as you don’t directly involve the Unity API); look up System.Threading.

Using String.split implies a fairly simple structure, so rolling your own string parsing for that shouldn’t be too bad. Either the IEnumerator like you suggested, or just put it in a coroutine and yield periodically. As long as you’re just pulling out small pieces, String.Substring should be fast no matter the size of the string since it’s a simple index and copy operation.

When searching for the next delimiter, use one of the String.IndexOf overloads that has the startIndex parameter.