Now i am doing the split after Readline method. But, my coding always got problem. Hope someone can help me...

Here is my coding

file = new StreamReader(fileOpen.FileName);
                        StringBuilder sb = new StringBuilder();

                        while ((line = file.ReadLine()) != null)
                        {
                            //MessageBox.Show(line,"TEST");
                            //textBox7.Text = line;
                            sb.AppendLine(line);
                        }
                            textBox7.Text = sb.ToString();
                            string temp1 = "";

                            foreach (string lines in               File.ReadAllLines(fileOpen.FileName))
                            {
                                 parts = lines.Split(new string[] { " ", "\n", "\r", "\t" }, StringSplitOptions.RemoveEmptyEntries);

                                foreach (string part in parts)
                                {
                                    temp1 = temp1 + "\n" + part;
                                }

                                
                               MessageBox.Show(temp1, "read by line");
                             }

who can help me..?

Recommended Answers

All 11 Replies

i tried already. But, still got problems in my coding.

What is it that you want to split the string? Do you want to get all words into an array?

I'm not made out of some divine substance.
I'm just the guy around the block.
So if you would be so kind to point out what sort of errors, that would be very nice to all of us here. :)

string in number (many lines for number).

for example,
1 2 3 4 5 (count as one line)
1 2 3 (second line)
....

need to split them and store in string array...

if (File.Exists(fileOpen.FileName))
                {
                    StreamReader file = null;
                    try
                    {
                        file = new StreamReader(fileOpen.FileName);
                        while ((line = file.ReadLine()) != null)
                        {
                            sb.AppendLine(line);
                            
                        }
                        //txtCaseInputs.Text = sb.ToString();

                        foreach (string byLine in line)
                        {
                            SplitItemLineByLine = (sb.ToString()).Split(new string[] { "\n\r\t", " " }, StringSplitOptions.None);

                            foreach (string lineByLine in SplitItemLineByLine)
                            {
                                ListSplitLineByLine += "\r\n" + lineByLine;
                            }
                        }
                        txtCaseInputs.Text = ListSplitLineByLine.Trim();
                        
                    }
                    catch (Exception o)
                    {
                        string Message = o.Message + "\n\nCannot open " + fileOpen.FileName;
                        MessageBox.Show(Message, "Open error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        return;
                    }
                }

This is the coding i did. But, this coding still got problem.

public String[] GetWordsFromFile(String filename) {
    List<String> myWords = new List<String>();
    if (File.Exists(filename)) {
        String[] myStrings = File.ReadAllLines(filename);

        foreach (String line in myStrings) {
            String[] words = line.Split(new char[] {' ', '\n', '\r', '\t'}, StringSplitOptions.RemoveEmptyEntries);
            foreach (String word in words) {
                myWords.Add(word);
            }
        }
    }

    return myWords.ToArray();
}

erm..still cannot.. i wan readline function because i need to count the number one by one from that many lines of numbers..

if (File.Exists(fileOpen.FileName))
                {
                    StreamReader file = null;
                    try
                    {
                        file = new StreamReader(fileOpen.FileName);
                        while ((line = file.ReadLine()) != null)
                        {
                            sb.AppendLine(line);
                            
                        }
                        //txtCaseInputs.Text = sb.ToString();

                        foreach (string byLine in line)
                        {
                            SplitItemLineByLine = (sb.ToString()).Split(new string[] { "\n\r\t", " " }, StringSplitOptions.None);

                            foreach (string lineByLine in SplitItemLineByLine)
                            {
                                ListSplitLineByLine += "\r\n" + lineByLine;
                            }
                        }
                        txtCaseInputs.Text = ListSplitLineByLine.Trim();
                        
                    }
                    catch (Exception o)
                    {
                        string Message = o.Message + "\n\nCannot open " + fileOpen.FileName;
                        MessageBox.Show(Message, "Open error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        return;
                    }
                }

This is the coding i did. But, this coding still got problem.

the error for the coding is"Cannot convert char to string". SO i try many times still cant get it.

It's not clear what you want to do. The code I posted will split all the strings into an array, but you say you "need to count the number one by one" and I don't know what you mean. Do you want each individual line as an array of strings? Is this a problem where you are required to use ReadLine()?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.