Reputation: 113
I have a string that we get from Excel data. The excel data looks something like this:
and when the string for this ends up being:
Abigail Hampton\tYXC37EGI8IV\t206\t\"in felis. Nulla tempor augue ac ipsum. Phasellus vitae mauris sit amet lorem semper auctor. Sskxsad assdie sk \"\"asda\"\" \r\nsoisdfi asdifsofid \"\"aasdfi dsio ck.\"\r\nTravis N. Carter\tHKZ93OEW1QO\t213\teu dolor egestas rhoncus. Proin nisl sem, consequat nec, mollis vitae, posuere\r\nLatifah B. Bryan\tJBL58YOF9OK\t236\tmauris erat eget ipsum.\r\n
This string is tab-delimited and any cell with new line characters is qualified by by the "
character which is also the escape character.
What I want to do is split this string into a list so that the first string in the list is the first row, the second string is the 2nd row, and the 3rd string is the third row.
Upvotes: 1
Views: 264
Reputation: 113
I figured this out. Zohar deserves the credit for pointing me to TextFieldParser
.
I'm surprised c# doesn't have something similar. Anyhow, this is how I solved this problem:
using Microsoft.VisualBasic.FileIO;
using System.IO;
string text; // This contains the text I posted in the question.
List<string[]> multilineString = new List<string[]>();
byte[] byteArray = Encoding.UTF8.GetBytes(text); // convert string to stream
MemoryStream stream = new MemoryStream(byteArray);
using (var myString = new TextFieldParser(stream))
{
myString.TextFieldType = FieldType.Delimited;
myString.SetDelimiters("\t");
myString.HasFieldsEnclosedInQuotes = true;
while (!myString.EndOfData)
{
string[] fieldArray;
try
{
fieldArray = myString.ReadFields();
multilineString.Add(fieldArray);
}
catch (Microsoft.VisualBasic.FileIO.MalformedLineException ex)
{
continue;
}
}
}
Thanks Zohar!
Upvotes: 1
Reputation: 672
List<string> cellsList = new List<string>();
cellsList.AddRange(myString.Split('\t'));
string lineString = "";
List<string> linesList = new List<string>();
for (int i = 0; i < cellsList.Count; i++)
{
lineString += cellsList[i];
if((i + 1)%4 == 0)
{
linesList.Add(lineString);
lineString = "";
}
}
Where myString is the string you provided above. Note that this works only if each line has exactly 4 cells!
Upvotes: 0