How to achieve key/value pair max accurately with simple string using c#

coder rock 196 Reputation points
2023-11-16T21:05:40.3933333+00:00
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
ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,299 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,350 questions
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. Bruce (SqlWork.com) 57,641 Reputation points
    2023-11-16T23:41:12.8466667+00:00

    you need more sample data. what happens if the name is "John W. Smith". you also need more address samples.


  2. SurferOnWww 1,996 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"}}


  3. QiYou-MSFT 4,306 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;
    
    
    

    PNG1

    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.