开发者

read from file and split the information based on a character c#

开发者 https://www.devze.com 2023-04-11 14:14 出处:网络
i am reading information from a text file and i want to go through the text file row by row and in each row i want to split each sentence from the other based on a character(eg. \',\') and i want to s

i am reading information from a text file and i want to go through the text file row by row and in each row i want to split each sentence from the other based on a character(eg. ',') and i want to save the data in an array but when i print it i am getting just the last result.

     private void button1_Click_1(object sender, EventArgs e)
    {
        string StringArray = null;
        //to get the browsed file and get sure it is not curropted
        try 
        {
            DialogResult result = openFileDialog1.ShowDialog();
            if (result == DialogResult.OK)
            {
                using (StreamReader sr = new StreamReader(openFileDialog1.FileName))
                {
                    string data;
                    while ((data = sr.ReadLine()) != nul开发者_开发百科l)
                    {
                    StringArray = data.Split(',');
                    }
                }
                   for (int i = 0; i < StringArray.Length; i++)
                    {
                        textBox1.Text = StringArray[i];
                    }
                FilePath.Text = openFileDialog1.FileName;
                textBox1.Text = (string)File.ReadAllText(FilePath.Text);
            }
        }
        catch(IOException ex) 
        {

            MessageBox.Show("there is an error" + ex+ "in the file please try again");
        }


    }


Here is your error:

Above you define:

string StringArray = null;

Then later you use it as:

StringArray = information.ToString().Split(SplitCommas);

Split returns string[] not string. you need to change the declaration at the top to..

string[] StringArray;

The error: "cannot implicitly convert a type string[] to string". should give you the hint that you are trying to store a string array into a string.


You need to define StringArray as a String[]

string[] StringArray = null;

You might be better off using String.Split instead of the for loop

StringArray = data.Split(',');


In your 3rd line you a declaring String array as a string, you should declare it as an array:

string [] StringArray = null;
0

精彩评论

暂无评论...
验证码 换一张
取 消