473,396 Members | 1,963 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Re-use a string array using the split method

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!!!!
Dec 6 '11 #1

✓ answered by adriancs

You mean...you want to split all the strings and make it a string array?

Expand|Select|Wrap|Line Numbers
  1. private void button1_Click(object sender, EventArgs e)
  2. {
  3.     string line = "";
  4.     string pathNames = "D:\\textfile.txt";
  5.     ArrayList rawData = new ArrayList();
  6.     try
  7.     {
  8.         StreamReader readFile = new StreamReader(pathNames);
  9.         while (line != null)
  10.         {
  11.             string[] sa = line.Split(' ');
  12.             foreach (string s in sa)
  13.             {
  14.                 if (s != "")
  15.                     rawData.Add(s);
  16.             }
  17.             line = readFile.ReadLine();
  18.         }
  19.     }
  20.     catch (Exception f)
  21.     {
  22.         MessageBox.Show(f.Message);
  23.     }
  24.     string da = "";
  25.     int rIn = rawData.Count;
  26.     string[] data = new string[rIn];
  27.     for (int i = 0; i < rIn; i++)
  28.     {
  29.         data[i] = rawData[i] + "";
  30.         da += "data[" + i + "] = " + rawData[i] +"\n";
  31.     }
  32.     MessageBox.Show(da);
  33. }

11 4458
adriancs
122 100+
check this out. Possible duplicate question:
http://bytes.com/topic/c-sharp/answe...e-bounds-array

for string split method examples:
http://www.dotnetperls.com/string-split
Dec 7 '11 #2
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.

Expand|Select|Wrap|Line Numbers
  1. ArrayList rawData = new ArrayList();
  2. try
  3. {
  4.   StreamReader readFile = new StreamReader(pathNames);
  5.   while (line != null)
  6.     {
  7.       line = readFile.ReadLine(); 
  8.       rawData.Add(line);
  9.     }
  10.   }
  11.     catch (Exception f)
  12.      {
  13.        MessageBox.Show(f.Message);
  14.      }
  15. int rIn = rawData.Count;
  16. for (int i = 0; i < rIn; i++)
  17.      {
  18.         string[] data = rawData[i].Split();
  19.  
  20. // Do a whole bunch of stuff, up to here everything works as expected
  21.      }
  22.  
  23. // I get:
  24. //       data[0] = "a1"
  25. //       data[1] = "a2"
  26. //       data[3} = "a3"
  27. // However it's on the next iteration i = 1, I get the following
  28. //       data[0] = "a1"
  29. //       data[1] = "a2"
  30. //       data[3} = "a3"
  31. //       data[4] = "b1"
  32. //       data[5] = "b2"
  33. //       data[6} = "b3"
  34. // And I just want
  35. //       data[0] = "b1"
  36. //       data[1] = "b2"
  37. //       data[3} = "b3"
  38.  
  39.  
How do I tell my program to start filling ‘data’ array from '0' again?
Dec 7 '11 #3
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.....
Dec 7 '11 #4
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.

Joe
Dec 7 '11 #5
adriancs
122 100+
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:\textfile.txt):
Expand|Select|Wrap|Line Numbers
  1. yellow blue green red
  2. tea juice milk coffee
  3. car bus lorry van
the code:
Expand|Select|Wrap|Line Numbers
  1. public partial class Form1 : Form
  2. {
  3.     public Form1()
  4.     {
  5.         InitializeComponent();
  6.     }
  7.  
  8.     private void button1_Click(object sender, EventArgs e)
  9.     {
  10.         string line = "";
  11.         string pathNames = "D:\\textfile.txt";
  12.         ArrayList rawData = new ArrayList();
  13.         try
  14.         {
  15.             StreamReader readFile = new StreamReader(pathNames);
  16.             while (line != null)
  17.             {
  18.                 line = readFile.ReadLine();
  19.                 rawData.Add(line);
  20.             }
  21.         }
  22.         catch (Exception f)
  23.         {
  24.             MessageBox.Show(f.Message);
  25.         }
  26.         int rIn = rawData.Count;
  27.         for (int i = 0; i < rIn; i++)
  28.         {
  29.             string[] data = (rawData[i] + "").Split(' ');
  30.             string da = "";
  31.             if (data.Length == 1 && data[0] == "") da = "Finish.";
  32.             else
  33.             {
  34.                 for (int j = 0; j < data.Length; j++)
  35.                 {
  36.                     da += "data[" + j + "] = " + data[j] + "\n";
  37.                 }
  38.             }
  39.             MessageBox.Show(da);
  40.         }
  41.     }
  42. }
Dec 7 '11 #6
Fr33dan
57
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:
Expand|Select|Wrap|Line Numbers
  1. rawData[0] = "a1,a2,a3"
  2. rawData[1] = "a1,a2,a3,b1,b2,b3"
  3. rawData[2] = "a1,a2,a3,b1,b2,b3,c1,c2,c3"
I have seen errors like this in reading text files when reusing the same variables when populating a data arrays.

Edit: seems adriancs and I were typing at the same time, but we're both getting at the same issue I think.
Dec 7 '11 #7
adriancs
122 100+
You mean...you want to split all the strings and make it a string array?

Expand|Select|Wrap|Line Numbers
  1. private void button1_Click(object sender, EventArgs e)
  2. {
  3.     string line = "";
  4.     string pathNames = "D:\\textfile.txt";
  5.     ArrayList rawData = new ArrayList();
  6.     try
  7.     {
  8.         StreamReader readFile = new StreamReader(pathNames);
  9.         while (line != null)
  10.         {
  11.             string[] sa = line.Split(' ');
  12.             foreach (string s in sa)
  13.             {
  14.                 if (s != "")
  15.                     rawData.Add(s);
  16.             }
  17.             line = readFile.ReadLine();
  18.         }
  19.     }
  20.     catch (Exception f)
  21.     {
  22.         MessageBox.Show(f.Message);
  23.     }
  24.     string da = "";
  25.     int rIn = rawData.Count;
  26.     string[] data = new string[rIn];
  27.     for (int i = 0; i < rIn; i++)
  28.     {
  29.         data[i] = rawData[i] + "";
  30.         da += "data[" + i + "] = " + rawData[i] +"\n";
  31.     }
  32.     MessageBox.Show(da);
  33. }
Dec 7 '11 #8
adriancs
122 100+
duplicate post.
This post can be deleted.
Dec 7 '11 #9
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 :-)
Dec 8 '11 #10
adriancs
122 100+
Hi, you have to mark "choose as best answer" button on the most appropriate post that has solved your problems. :-)
Dec 9 '11 #11
Sorry - Done :-) :-)
Dec 9 '11 #12

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Craig Bailey | last post by:
Anyone recommend a good script editor for Mac OS X? Just finished a 4-day PHP class in front of a Windows machine, and liked the editor we used. Don't recall the name, but it gave line numbers as...
1
by: Chris | last post by:
Sorry to post so much code all at once but I'm banging my head against the wall trying to get this to work! Does anyone have any idea where I'm going wrong? Thanks in advance and sorry again...
11
by: James | last post by:
My form and results are on one page. If I use : if ($Company) { $query = "Select Company, Contact From tblworking Where ID = $Company Order By Company ASC"; }
8
by: Beowulf | last post by:
Hi Guru's, I have a query regarding using PHP to maintain a user profiles list. I want to be able to have a form where users can fill in their profile info (Name, hobbies etc) and attach an...
3
by: David Hickman | last post by:
Hi all, This is a newbie question but I have been struggling with this for ages. I have a document with 2 separate <?php sections of code. I would like to declare some variables in the first...
2
by: JW | last post by:
I wanted have this as part of a flood control script: <? echo ("Flood control in place - please wait " . $floodinterval . " seconds between postings."); sleep(5); // go back two pages echo...
10
by: Lukas | last post by:
I want to send an email directly to a directory on my server what script do I need to do this? Maybe I should add a line to my aliases file like this: emailaddress: "|/usr/bin/email2directory" ?...
6
by: Chris Krasnichuk | last post by:
Hello every one, Does anyone know how to make php work on your computer? please reply I need help Chris
2
by: Frans Schmidt | last post by:
I want to make a new database with several tables, so I did the following: <?php CREATE DATABASE bedrijf; CREATE TABLE werknemers (voornaam varchar(15), achternaam varchar(20), leeftijd...
2
by: sky2070 | last post by:
Parse error: parse error, unexpected T_OBJECT_OPERATOR, expecting ')' in c:\inetpub\wwwroot\session.php on line 19 can anyone tell me what is wrong with this code??? <? // Define the Session...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.