Click here to Skip to main content
15,891,431 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all,

I am reading a txt file from C# app. When it reads the
first line (and then the next etc..) it gets trown into the "line" string.(line
by line)

In my txt file, in the line I seperated the values using the "*"
So I need it to break up the line into seperate values. When it
sees a * it must know it is the end of that value.

So insted of line by line, I want it value by value.

Please any help would be appriciated!!

Here is the code I used:

C#
int counter = 0;
            string line;

            // Read the file and display it line by line.
            System.IO.StreamReader file =
               new System.IO.StreamReader("C:/Users/user/Downloads/test1.txt");
            while ((line = file.ReadLine()) != null)
            {
                MessageBox.Show(line);
                counter++;
            }

            file.Close();
Posted

Use the String.Split[^] method.

Regards,

Manfred
 
Share this answer
 
Comments
Shahin Khorshidnia 6-Mar-12 5:19am    
Hi, maybe you want to revote my solution.
Shahin Khorshidnia 6-Mar-12 6:08am    
+5
I have created a file named 1.txt having value

VB
asd*fasfda*fdaaaaa*aaaaaaaaa
dddddddd*ddd



Now for separating values based on "*" I have used following code
static void Main(string[] args)
{
    string text="";
    text = System.IO.File.ReadAllText(@"F:\Test\1.txt");
    string[] Values = text.Split('*');

    int counter = 0;
    while (counter < Values.Length)
    {
        Console.Write(Values[counter++]);
        Console.Write("\n");
    }

}
 
Share this answer
 
Comments
Shahin Khorshidnia 6-Mar-12 6:06am    
+5 I think that's a good solution too and I don't know who "and why" is downvoting other solutions (without any reason)
Do not use ReadLine()

Read the whole file as a string value. Then separate it by *


C#
System.IO.StreamReader file =
     new System.IO.StreamReader("C:/Users/user/Downloads/test1.txt");


string wholeFile = file.ReadToEnd();

string[] separatedFile = wholeFile.Split('*');
 
Share this answer
 
v2
Comments
Ben Paxton 6-Mar-12 5:27am    
Thanks all good so far, but I need every value into a seperate string, I am going to use this to save it to SQL DB
Shahin Khorshidnia 6-Mar-12 6:07am    
Thanks for accepting.
It's already into separates strings.
I suggested array.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900