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

  • SEARCH
  • Home
  • 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 8705323
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T03:19:57+00:00 2026-06-13T03:19:57+00:00

I have a string1, string2,…,stringn and values of each strings are string1 = s1k1-s1k2-s1k3-….-s1kn

  • 0

I have a string1, string2,...,stringn
and values of each strings are

string1 = "s1k1-s1k2-s1k3-....-s1kn | s1v1-s1v2-s1v3-....-s1vn";

string2 = "s2k1-s2k2-s2k3-....-s2kn | s2v1-s2v2-s2v3-....-s2vn";

                .............

stringn = "snk1-snk2-snk3-....-snkn | snv1-snv2-snv3-....-snvn";

Now, I want to populate the Dictionary<string, string>() with the key and values as follows.

var dict = new Dictionary<string, string>();
dict.Add(s1k1, s1v1);
dict.Add(s1k2, s1v2);
         :
         :
dict.Add(snkn, snvn);

Below is my sample code:

        string string1 = "s1k1-s1k2-s1k3-s1kn|s1v1-s1v2-s1v3-s1vn";
        string string2 = "s2k1-s2k2-s2k3-s2kn|s2v1-s2v2-s2v3-s2vn";
        string stringn = "snk1-snk2-snk3-snkn|snv1-snv2-snv3-snvn";

        var dict = new Dictionary<string, string>();

        new List<string> { string1, string2, stringn }.ForEach(str =>
        {
            var strSplit = str.Split('|');
            var strKeys = strSplit[0].Split('-');
            var strValues = strSplit[1].Split('-');
            strKeys.Zip(strValues, (key, value) => new { key, value }).ToList().ForEach(filed => dict.Add(filed.key, filed.value));
        });

Is it possible to optimize this code ?

  • 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-06-13T03:19:59+00:00Added an answer on June 13, 2026 at 3:19 am

    To my mind the most costly operation here in terms of memory usage is strSplit = str.Split('|'). It creates two large strings that go directly to a garbage after strKeys and strValues are obtained. Later all these garbage strings will be collected by garbage collector which will negatively affect the performance.

    The strKeys and strValues string arrays contain strings that will be used in dict, so the memory loss here is minimal.

    To optimize memory usage you’ll have to traverse str char-by-char and create keys and values using StringBuilder class. In this case memory loss will be minimal, however your code won’t be as clean as it is now. So I would recommend such solution only if this part of the code is the real performance bottleneck in your application.

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

Sidebar

Related Questions

I have a file strings.txt containing 100 strings, each on a line string1 string2
I have a table view with a list of strings as follows: String1 String2
I have a file containing on each line a string of the form string1.string2:\string3{string4}{number}
I have two strings, string1 and string2. I want to check if string1 can
I have this: NSString *string1 = ...; NSString *string2 = ...; NSMutableArray *array =
I have seven strings in a program named string1 through string7. The size of
x=1 c1=string1 c2=string2 c3=string3 echo $c1 string1 I'd like to have the output be
I have a method with multiple variables: -(void) showImg:(UIBarButtonItem *)sender string1:(NSString *) string2:(NSString *);
I have the following : ViewBag.SomeEnumerable = new List<string>() { string1, string2 }; Now
I m using mysql. in one field i have inserted data like string1::string2 two

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.