views:

122

answers:

6
+2  Q: 

Parse line of text

Hi all,

I have thousands of text files, and each file contains just a single line of text.

Any single file looks like this:

somevalue1|somevalue2|somevalue3|somevalue4|somevalue5

The format never changes, and from above, you can see how each value is separated by a "|". And there are always exactly 5 values in each file.

Can someone please help me in the right direction? I'm not sure how I could go about extracting each value, into a separate string, like:

string value1,value2,value3,value4,value5;
value1=somevalue1;
value2=somevalue2; 

etc etc.

I hope this makes sense. And appreciate any help at all

Thank you

+12  A: 

String.Split does that for you:

string line = "somevalue1|somevalue2|somevalue3|somevalue4|somevalue5";
string [] parts = line.Split(new[] { '|' });

Then you can read the separate values from that array:

string value1 = parts[0];
string value2 = parts[1];
// and so on

Now I might drift slightly off topic, but I assume that the different values have different meaning, so that they can be regarded as columns in a table? If that is the case, I might make sense to create a class that exposes these values in a more discoverable form. Let's pretent that the five values are first name, last name, company, favorite color and name of pet unicorn. Then you could create the following class:

class Person
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Company { get; set; }
    public string FavoriteColor { get; set; }
    public string NameOfPetUnicorn { get; set; }
}

...and you would then put the values from the line into such a person like so:

private static Person GetPerson(string line)
{
    string [] parts = line.Split('|'); // as per Dan Tao's suggestion
    return new Person {
        FirstName = parts[0],
        LastName = parts[1],
        Company = parts[2],
        FavoriteColor = parts[3],
        NameOfPetUnicorn = parts[4]
    };
}
Fredrik Mörk
You can also write simply: `line.Split('|')` (since the separator is a `params` argument).
Dan Tao
@Dan: good call. That removes some noise from the code.
Fredrik Mörk
+1  A: 

String.Split lets you split a string on a specific character (or set of characters if needed). It returns a string[] of the string values.

Brian Rasmussen
+1  A: 

IF the format is always consistent, you should read the contents of the file to end of file into one string, then use String.split() to split into your component strings with the delimiting char ( which is | in this case).

string.split http://msdn.microsoft.com/en-us/library/b873y76a.aspx

JohnnyH
+1  A: 
    // for each file
    string contents = ""; // read in the whole file into this variable
    foreach (string line in contents.Split(Environment.NewLine.ToCharArray(), StringSplitOptions.RemoveEmptyEntries))
    {
        string[] values = line.Split('|'); 
        // do something with the values, accessing the first one as values[0] etc
    }
ho1
+1  A: 

Easiest way is a string split:

string input = "somevalue1|somevalue2|somevalue3|somevalue4|somevalue5"; string[] someValues = input.Split('|');

http://msdn.microsoft.com/en-us/library/y7h14879.aspx

BrokenGlass
+1  A: 

If you are looking for some general solution you can try FileHelpers.

The FileHelpers are a free and easy to use .NET library to import/export data from fixed length or delimited records in files, strings or streams.

With FileHelpers you can define file record as following:

[DelimitedRecord ("|")]
public class DataFileRecord
{
    public string SomeValue1 { get; set; }
    public string SomeValue2 { get; set; }
    public string SomeValue3 { get; set; }
    public string SomeValue4 { get; set; }
    public string SomeValue5 { get; set; }
}

/*reading section*/
var engine = new DelimitedFileEngine<DataFileRecord> ();
DataFileRecord[] records = engine.ReadFile (path);
jethro