HOW to split string? or "advanced" editing string variable? C#

Hello,

I have made map editor that saves all map information in one string, then that string is saved as file in some directory, but when I load map, I load that string which is full of informations that I need splited:

string text is string that is saved as file with map info
so if I have

text = “GroundChunk_5_(0,0,0)(1,0,0)(-1,0,0)(0,0,1)(0,0,-1)”

now I load that text, and I want to split that string on other strings so I can actually use that informations:

string blockType = “GroundChunk”; //TAKEN from “text” string (1st information)
string blockNumber = 5; //TAKEN from “text” string (2nd information)

and so on


before unity I was programming maps in warcraft 3 and there it’s really well made, easy to use,there you can cut string (like remove first 5 characters or last characters; as many as you want) so at the end, in new string you get only characters that you need from the first string variable.

You can use a TextReader to open the file and in a streaming way read and parse different elements:

One thing it’s missing is a ReadUntil function, where it reads up until a specified delimiter, but that is easy to create using TextReader.Read() function and the StringBuilder class.

The other more bruteforce method is String.Split()

Also: String.Substring

Which can be used with the string.IndexOf() functions:

Check the string class, StringBuilder class, and TextReader class, and it can do all you need and much more.

You can split a string with string.split as Ntero said, here a quick example:

string n_s = "John_Smith";
string name = "";
string surname = "";

void Start()
{
       string[] splitArray =  n_s.Split(n_s,char.Parse("_")); //Here we assing the splitted string to array by that char
       name = splitArray[0]; //Here we assign the first part to the name
       surname = splitArray[1]; //Here we assing the second part to the surname
}