开发者

.net way to split odd and even pipe-delimeted data

开发者_开发技巧I have a bunch of data in pipe-delimited format, where odd entries are index numbers, and even entries are the data e.g.

1|cat|2|dog|3|manatee|4||5|gerbil|6|etc

(note occasional missing values)

and I'm wondering if there is a nice .NET type way of turning this into a class pet with id and name parameters.

I've got some code which does a String.Split and iterates over the array, scraping together objects, but it looks more like I'm writing PL/SQL than C#... I'm sure there is a way of doing this in a single line of Linq or something - can anyone tell me the 'right' way to do this?

Thanks


You can use the TextFieldParser class to handle the parsing of the string into fields (I know it's a VB class, but you can access it from C#). Then just set alternating fields to your new objects.

Here's the example from MSDN, changed into C#:

using (Microsoft.VisualBasic.FileIO.TextFieldParser MyReader = 
       new Microsoft.VisualBasic.FileIO.TextFieldParser("C:\\testfile.txt")) 
   { 
        MyReader.TextFieldType = FileIO.FieldType.Delimited; 
        MyReader.SetDelimiters(","); 
        string[] currentRow = null; 
        while (!MyReader.EndOfData) 
        { 
            try 
            { 
                currentRow = MyReader.ReadFields(); 
                string currentField = null; 
                foreach (var currentField in currentRow) 
                { 
                    //set values for your object here
                } 
            } 
            catch (Microsoft.VisualBasic.FileIO.MalformedLineException ex) 
            { 
              //handle the exception
            } 
        } 
    } 


This is how I did it, which works, but doesn't seem very '.NET' way of doing it...

public List<Pet> CreatePets(String PetData)
{
     List<Pet> PetList = new List<Pet>();
     string[] PetArray = PetData.Split(new char[]{'|'}, StringSplitOptions.None);
     for (int i = 0; i < PetArray.Count(); i += 2)
     {
         Pet NewPet= new Pet(Convert.ToInt32(PetArray[i]), PetArray[i+1]);
         PetList.Add(Field);
     }                
     return PetList;
}


Here is my solution for splitting string arrays with "|". It's worked fine with GridView's colunms.

Public Function GridViewColumnsWidths(ByVal Gridview As GridView, ByVal ColumnNameCommaWidth As String) As Boolean
                Dim i As Boolean
                Dim ArrayOfItem As String() = Split(ColumnNameCommaWidth, "|",, CompareMethod.Text) 'eg: "ColA|200|ColB|100|ColC|300"
                For IndexOfItem As Integer = 0 To ArrayOfItem.Count - 1
                    Dim Column As String = ArrayOfItem(IndexOfItem)
                    Dim Width As Integer = Convert.ToInt16(ArrayOfItem(IndexOfItem + 1))
                    With Gridview.Columns
                        .Item(Column).Width = Width
                    End With
                    IndexOfItem += 1
                Next
                Return i
            End Function
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜