- Home /
Loading a text document without spaces
For my project I need to load a very large document. The document does not have spaces between the characters, but it does have many lines. My problem comes from the line: lines = Regex.Split(text, string.Empty); This only seperates the characters by lines leading to a single long row of objects instead of a maze. How do I fix this?
string text = System.IO.File.ReadAllText("map.txt");
string[] lines = Regex.Split(text, string.Empty);
int rows = lines.Length;
string[][] levelBase = new string[rows][];
for (int i = 0; i < lines.Length; i++) {
string[] stringsOfLine = Regex.Split(lines[i], " ");
levelBase[i] = stringsOfLine;
}
string[][] jagged = levelBase;
for (int y = 0; y < jagged.Length; y++) {
for (int x = 0; x < jagged[0].Length; x++) {
Debug.Log(jagged[0].Length );
switch (jagged[y][x]){
case sfloor_valid:
Instantiate(floor_valid, new Vector3(x, -.2f, -y), Quaternion.identity);
break;
case sfloor_obstacle:
Instantiate(floor_obstacle, new Vector3(x, 0, -y), Quaternion.identity);
break;
case sfloor_oob:
Instantiate(floor_OutOfBounds, new Vector3(x, 0, -y), Quaternion.identity);
break;
}
}
}
}
I have, but found nothing that dealt with both a lack of spaces btw characters and more than a single line
is there some ter$$anonymous$$ator at the end of each line within this 'very large' document? (how large is very large?)
splitting the text with String.Empty
ins$$anonymous$$d of that ter$$anonymous$$ator character is never going to work...
no ter$$anonymous$$ator at the end and not allowed to edit the document
Answer by robertbu · Nov 01, 2014 at 02:07 AM
I think you are going about this the hard way. You can do:
string text = System.IO.File.ReadAllText("map.txt");
string[] lines = lines = text.Split (new[] { '\r', '\n' }, System.StringSplitOptions.RemoveEmptyEntries);
As with your code above, you have an array of strings. I use this particular split methods since the result will work for both Mac/Unix and Windows line endings, and produce an array without any empty lines. You can access any specific character just like the jagged array you are trying to produce. For example, you can do:
Debug.Log(lines[2][2]);
...which will display the third character in the third line. If you really want a jagged character array, you can process 'lines' array produced in the previous step as follows:
char[][] data;
data = new char[lines.Length][];
for (int i = 0; i < lines.Length; i++) {
data[i] = lines[i].ToCharArray();
}