I have a list of input strings and I want to take each string and using the split method populate a string array, do some stuff with it and that process is in a "for" loop. After each pass through the "for" loop I want to re-use the same string array. And, as all you fundi's in c# will know, the array just keeps getting added to. How do I say start from scratch each time? I know this is going to be real easy, and I'm going to feel real foolish once it is pointed out to me, but please point it out to me!!!!
Re-use a string array using the split method
Collapse
X
-
Tags: None
-
check this out. Possible duplicate question:
for string split method examples:
-
Thanks Adriancs, but doesn't answer my question. It was using dotnetperls that gave me the idea to use the split method. Say that I read, using StreamReader, strings of data into memory:
a1 a2 a3
b1 b2 b3
c1 c2 c3 etc.
Code:ArrayList rawData = new ArrayList(); try { StreamReader readFile = new StreamReader(pathNames); while (line != null) { line = readFile.ReadLine(); rawData.Add(line); } } catch (Exception f) { MessageBox.Show(f.Message); } int rIn = rawData.Count; for (int i = 0; i < rIn; i++) { string[] data = rawData[i].Split(); // Do a whole bunch of stuff, up to here everything works as expected } // I get: // data[0] = "a1" // data[1] = "a2" // data[3} = "a3" // However it's on the next iteration i = 1, I get the following // data[0] = "a1" // data[1] = "a2" // data[3} = "a3" // data[4] = "b1" // data[5] = "b2" // data[6} = "b3" // And I just want // data[0] = "b1" // data[1] = "b2" // data[3} = "b3"
Comment
-
Hello Joe ,
I am not sure if this works...
Declare the string array as
string[] data = null;
outside the for loop or try adding
data = null;
just before the closing brace of for loop.....Comment
-
Hello Shilpa,
Thanks for trying, but no, it doesn't work. To be honest I had already tried the data = null rout inside the for loop, but this time I added the string data[] = null also, but without any success, but thank you for making the suggestion.
JoeComment
-
well, perhaps there's some logic error occur somewhere at the unseen code.
I have try this, and it seems to be what you want.
you may need to double check your coding.
I have test the below with a text file.
The content of the text(D:\textfil e.txt):
Code:yellow blue green red tea juice milk coffee car bus lorry van
Code:public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { string line = ""; string pathNames = "D:\\textfile.txt"; ArrayList rawData = new ArrayList(); try { StreamReader readFile = new StreamReader(pathNames); while (line != null) { line = readFile.ReadLine(); rawData.Add(line); } } catch (Exception f) { MessageBox.Show(f.Message); } int rIn = rawData.Count; for (int i = 0; i < rIn; i++) { string[] data = (rawData[i] + "").Split(' '); string da = ""; if (data.Length == 1 && data[0] == "") da = "Finish."; else { for (int j = 0; j < data.Length; j++) { da += "data[" + j + "] = " + data[j] + "\n"; } } MessageBox.Show(da); } } }
Comment
-
You have you debugged and verified the status of rawData? The code as you posted it throws away the data[] variable every time split is called, just as if you set an int from 3 to 5, the 3 is thrown out.
In order to get what you described the next instance of rawData must have the data from the previous in it:
Code:rawData[0] = "a1,a2,a3" rawData[1] = "a1,a2,a3,b1,b2,b3" rawData[2] = "a1,a2,a3,b1,b2,b3,c1,c2,c3"
Edit: seems adriancs and I were typing at the same time, but we're both getting at the same issue I think.Comment
-
You mean...you want to split all the strings and make it a string array?
Code:private void button1_Click(object sender, EventArgs e) { string line = ""; string pathNames = "D:\\textfile.txt"; ArrayList rawData = new ArrayList(); try { StreamReader readFile = new StreamReader(pathNames); while (line != null) { string[] sa = line.Split(' '); foreach (string s in sa) { if (s != "") rawData.Add(s); } line = readFile.ReadLine(); } } catch (Exception f) { MessageBox.Show(f.Message); } string da = ""; int rIn = rawData.Count; string[] data = new string[rIn]; for (int i = 0; i < rIn; i++) { data[i] = rawData[i] + ""; da += "data[" + i + "] = " + rawData[i] +"\n"; } MessageBox.Show(da); }
Comment
-
Thanks guys, you have pointed me in the right direction, the code I didn’t show. Adrian, for brevity purposes I was not totally expicit, the actual code works on a split of a split. So, for the first split I also get the same result with the code I did not supply as you get with the code you supplied. It's when I get to the next step that I ran into the problem, and, as Hamlet say’s, therein lies the rub! I have been sticking the result of my first split into a string, and that code was wrapped in a #region/#endregion , and out of sight out of mind, I have not been clearing out the string from the first split. I think I am now going to hang myself :-)Comment
-
Comment