c# - Split a line of strings into strings consisting of two characters -
c# - Split a line of strings into strings consisting of two characters -
i reading in text file consists of grid of alpha-numeric values (see below).
iqqqqq wg2223 s22228 d22223
currently these values looped through , each character sent switch case. switch reads character , outputs given result. code process follows.
private void loadlevel(stream stream) { list<string> lines = new list<string>(); uint width; using (streamreader reader = new streamreader(stream)) { string line = reader.readline(); width = (uint)line.length; while (line != null) { lines.add(line); line = reader.readline(); } } tiles = new tile[width, lines.count]; (int y = 0; y < height; ++y) { (int x = 0; x < width; ++x) { char type = lines[y][x]; tiles[x, y] = loadtile(type, x, y); } } }
in code retrieve text file , store each line in list , loop through each line , extract each character @ given point in grid. rather extract single character extract 2 characters @ same time , pass loadtile function.
as illustration take first line of grid.
iqqqqq
i split line 3 strings each 2 characters long , pass loadtile , go on looping through remainder of grid. not know begin effort accomplish task. help appreciated.
well first off you'll propably want alter signature of loadtile
loadtile(char, int, int)
loadtile(string, int, int)
. alter calculation of width to
width = line.length/2;
of course of study if line has odd number of characters you'll lose lastly character. additionally if line after first shorter you'll have exceptions , if longer you'll lose additional data.
then can loop through , take substrings.
for (int y = 0; y < height; ++y) { (int x = 0; x < width; ++x) { string type = lines[y].substring(x*2,2); tiles[x, y] = loadtile(type, x, y); } }
c# regex linq stream
Comments
Post a Comment