Read string enclosed in double quotes from text file
I have a text file that contains a data like
ID Name Path IsTrue Period
1 "1 yr" "C:\\Program Files\\My File.xyz" -1 2"
1 "1 yr" "C:\\Program Files\\My File.xyz" -1 2"
now I have the following code to split the line
string[] ArrSeperators = { " " };
ArrSplitStrs = CurrStr.Split(ArrSeperators,
StringSplitOptions.RemoveEmptyEntries);
CurrStr represents each line of text file.
T开发者_如何学运维he problem is it split the name and path into multiple string but they must be treated as a single string. I cannot make any changes to file as it is a standard file across different products.
I am not getting what I can do.
Use an algorithm like this:
Process each character of each line one at a time.
Count every " that you find.
If the number of "s is odd, you know that you need to keep reading the current field until you hit another ".
If the number of "s is even, you know that as soon as you hit a space you're on to the next field.
Something like (this may have errors - I've just written it off the top of my head):
StringBuilder field = new StringBuilder();
int quoteCount = 0;
foreach (char c in line)
{
if (c == '"')
{
quotCount++;
continue;
}
if (quoteCount % 2 = 0)
{
if (c == ' ')
{
yield return field.ToString();
field.Length = 0;
}
else
{
field.Append(c);
}
}
else
{
field.Append(c);
}
}
EDIT:
Here's a hacky example that works for your sample - the GetFields method needs some refactoring and it's far from the quality of anything I'd put in my code, but the basic principle is there.
class Program
{
static void Main(string[] args)
{
var records = ReadFile(@"D:\x.txt");
foreach (var record in records)
{
foreach (var field in record)
{
Console.Write(field + " | ");
}
Console.WriteLine();
}
Console.ReadKey();
}
static IEnumerable<IEnumerable<String>> ReadFile(String file)
{
using (var reader = new StreamReader(file))
{
// Ignore column titles line.
reader.ReadLine();
while (!reader.EndOfStream)
{
yield return GetFields(reader.ReadLine());
}
}
}
static IEnumerable<String> GetFields(String line)
{
Int32 quoteCount = 0;
StringBuilder field = new StringBuilder();
foreach (var c in line)
{
if (c == '"')
{
quoteCount++;
continue;
}
if (quoteCount % 2 == 0)
{
if (c == ' ')
{
if (field.Length > 0)
{
yield return field.ToString();
field.Length = 0;
}
}
else
{
field.Append(c);
}
}
else
{
field.Append(c);
}
}
yield return field.ToString();
}
}
If a tab separator is used to separate the fields you could use '\t'.
Try the following code. Tested with the sample provided in question...
string CurrStr = "1 \"1 yr\" \"C:\\Program Files\\My File.xyz\" -1 2\"";
string[] ArrSplitStrs = CurrStr.Split('"');
int HighestCount = ArrSplitStrs.Count() % 2 == 0 ? ArrSplitStrs.Count() : ArrSplitStrs.Count() - 1;
for (int Counter = 1; Counter < HighestCount; )
{
Console.WriteLine(ArrSplitStrs[Counter]);
Counter += 2;
}
精彩评论