I have simple string value line which contain below content.
logo Name raj mobile 9038874774 address 6-98 india bill auto generated
Now I am trying key/Value pair to achieve my detail and the pair value output expecting like below
[0] Key: Name value:Raj
[1] Key: Mobile value:9038874774
[2] Key: Address value:6-98 india
Below is code trying to achieve requirement
string[] lines = new string[] { "logo Name raj mobile 9038874774 address 6-98 india bill auto generated" };
// Get the position of the empty sign within each line
var pairs = lines.Select(l => new { Line = l, Pos = l.IndexOf(" ") });
// Build a dictionary of key/value pairs by splitting the string at the empty sign
var dictionary = pairs.ToDictionary(p => p.Line.Substring(0, p.Pos), p => p.Line.Substring(p.Pos + 1));
// Now you can retrieve values by key:
var value1 = dictionary["Name"];
Below is output looks like into debugger
The text string have contain some non-required words like logo and bill auto generated no need of this words to required into key/value pairs. Please suggest how to achieve this max accurately and data of string getting from image file converted into text string using terrasact OCR
2
Answers
Here’s an example using string.Split. I changed
line
to match the case of the keys, so you might have to deal with case issues. Also, I’m assuming Bill is a key that is safe to be ignored (same concern that @Mark Seemann raised in the comments.)There are other potential key issues though, for example, what if the name value is Bill?
This really looks like a parsing problem, but a quick-and-dirty implementation might be something like this:
Here, I’m assuming that
"bill"
is also a sort of key that should just be ignored.