Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 975611
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T03:36:45+00:00 2026-05-16T03:36:45+00:00

I have a list like List<string> TempList = new List<string> { [66,X,X], [67,X,2], [x,x,x]

  • 0

I have a list like

List<string> TempList = new List<string> { "[66,X,X]", "[67,X,2]", "[x,x,x]" };

I need to add data to the dictionary from the above list

Dictionary<int, int> Dict = new Dictionary<int, int>();

so the Dict should contain

Key --> 66 value --> 67

i need to take 66(first value) from first string([66,X,X]) and 67(first value) from second string( [67,X,X]) and add it as a key value pair into the dictionary.

Now i’m following string replacing and looping methodology to do this .

Is there any way to do this in LINQ or Regular expression.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T03:36:46+00:00Added an answer on May 16, 2026 at 3:36 am

    Here is an example using both string.Split() and a Regex:

    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                List<string> data = new List<string>() { "[66,X,X]", "[67,X,2]", "[x,x,x]" };
                addToDict(data);
    
                Console.ReadKey();
            }
    
            private static void addToDict(List<string> items)
            {
                string key = items[0].Split('[', ',')[1];
                string val = items[1].Split('[', ',')[1];
    
                string pattern = @"(?:^\[)(\d+)";
                Match m = Regex.Match(items[0], pattern);
                key = m.Groups[1].Value;
                m = Regex.Match(items[1], pattern);
                val = m.Groups[1].Value;
    
                _dict.Add(key, val);
            }
    
            static Dictionary<string, string> _dict = new Dictionary<string, string>();
        }
    }
    

    i suspect that your example is quite contrived though, so there may be a better solution especially if you need to process large numbers of strings into key/value pairs (i deliberately hardcoded index values because your example was quite simple and i didn’t want to over complicate the answer). If the input data is consistent in format then you can make assumptions like using fixed indexes, but if there is a possibility of some variance then there may need to be more code to check the validity of it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a list like this Dim emailList as new List(Of String) emailList.Add(one@domain.com) emailList.Add(two@domain.com)
I have a List of String like List<String> MyList=new List<String>{A,B}; and a Dictionary<String, Dictionary<String,String>>
I have a string like this: TEST.DATA.Data.COR.Point,2;TEST.DATA.Data.COR.Point,5;TEST.DATA.Data.COR.Point,12;TEST.DATA.Data.COR.Point,12;TEST.DATA.Data.COR.WordTOFIND,18 I have a list of array with
I have a collection like this List<int> {1,15,17,8,3}; how to get a flat string
I have a list like: List<String> test = new List<String> {Luke, Leia}; I would
i have a class like class CXmlData { String Data1=; String Data2=; List<String> lst=new
I have a list of String List<String> lst=new List<String>{A,B,C} And an xml file like
Some open source libraries have tendency to re implement basic structures like string, list,
I have a String List with items like this Root Root/Item1 Root/Item2 Root/Item3/SubItem1 Root/Item3/SubItem2
E.G. to create an ArrayList of Strings we have to do something like List<String>

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.