you need more sample data. what happens if the name is "John W. Smith". you also need more address samples.
How to achieve key/value pair max accurately with simple string using c#
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
https://i.stack.imgur.com/1FyJS.png
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 additional answers
Sort by: Most helpful
-
SurferOnWww 2,576 Reputation points
2023-11-17T01:05:30.1833333+00:00 Why don't you consider a use of the delimiter to split the string like below:
logo, Name, raj, mobile, 9038874774, address, 6-98 india, bill, auto generated
or the JSON format like below:
{"logo":{"Name":"raj","mobile":"9038874774","address":"6-98 india","bill":"auto generated"}}
-
QiYou-MSFT 4,311 Reputation points Microsoft Vendor
2023-11-17T06:00:50.27+00:00 Hi @coder rock
You can try to slice strings based on keywords.
string[] separatingStrings = {"logo", "Name", "mobile","address","bill" }; string str = "logo Name raj mobile 9038874774 address 6-98 india bill auto generated"; string[] words = str.Split(separatingStrings, StringSplitOptions.RemoveEmptyEntries); words[0] = "Name:" + words[1]; words[1] = "Mobile:" + words[2]; words[2] = "Address:" + words[3]; words[3] = "Bill:" + words[4]; words[4] = null;
Best regards,
Qi You
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.