Reputation: 447
I receives data in this format.
var data="frame= 159 fps= 51 q=34.0 size= 512kB time=00:00:05.37 bitrate= 780.3kbits/s dup=3 drop=0 speed=1.72x";
To use the delimiter here the only possibility is space.
But for that frame= 159
which has more spaces after = creating the problem.
Same problem with frame, size, bitrate.
So I can't use either delimiter or simple string split i.e data.Split(" ")
How to parse values here to correct DTO/Object.
public class Template
{
public float Frame {get;set;}
public float Fps {get;set;}
public string Size {get;set;}
public TimeSpan Time {get;set;}
public string Bitrate {get;set;}
public short Dup {get;set;}
public short Drop {get;set;}
public string Speed {get;set;}
}
Upvotes: 0
Views: 63
Reputation: 71
you can parse your string
to <key,value>
collection by remove unnecessary spaces:
var data="frame= 159 fps= 51 q=34.0 size= 512kB time=00:00:05.37 bitrate= 780.3kbits/s dup=3 drop=0 speed=1.72x".Replace(" ", " ");
data = Regex.Replace(data, @"[^\S]+", " ");
data = Regex.Replace(data, @"= ", "=");
var pairs = data.Split(" ");
var dictionary = new Dictionary<string, string>();
foreach (var pair in pairs)
{
var keyAndValue = pair.Split("=");
if (keyAndValue.Length == 2)
{
dictionary.Add(keyAndValue[0], keyAndValue[1]);
}
else
{
throw new InvalidOperationException();
}
}
Upvotes: 0
Reputation: 6524
remove spaces after =
using Regex
var data = "frame= 159 fps= 51 q=34.0 size= 512kB time=00:00:05.37 bitrate= 780.3kbits/s dup=3 drop=0 speed=1.72x";
data = Regex.Replace(data, @"=\s+", "=");
foreach (var d in data.Split(' '))
{
var kv = d.Split('=');
Console.WriteLine(String.Format("{0}: {1}", kv[0], kv[1]));
}
Upvotes: 1