I need a code which will print union of two strings. for example
string1="1,2,3"
string2="3,4,5"

and print the result string3: 1,2,3,4,5

Could someone help me how to solve this?

Recommended Answers

All 7 Replies

how do you mean print it ? output it to a console window ?

If you'r asking something like this you clearly haven't searched it in Google.

string string3 = string1 + string2;

Console.WriteLine("" + string3);

OR

Console.WriteLine("" + string1 + string2);

how do you mean print it ? output it to a console window ?

If you'r asking something like this you clearly haven't searched it in Google.

string string3 = string1 + string2;

Console.WriteLine("" + string3);

OR

Console.WriteLine("" + string1 + string2);

You shouldn't need to concatenate "" with the strings in .Writeline()

Also: String.Join

I read in this post that using + to concatenate strings is:

about 25% faster than using string.join()
about 15% faster than using string.format()
and about 10% faster than using a stringbuilder

Haven't actually tested it myself but this is some food for thought. Doesn't seem like much (microseconds per operation) but when you require large scale applications (ie a server that parses strings) this is an interesting place to start the optimizing.

Now that everyone has gone off track to your question :)

You'll need to parse out the values from the string (as ints, strings, characters, whatever). You can then use Linq to get the union of the two sets, for example

String string1 = "1,2,3";
String string2 = "3,4,5";

String[] s1Parts = string1.Split(',');
String[] s2Parts = string2.Split(',');

IEnumerable<String> union = s1Parts.Union(s2Parts);

foreach (String s in union) {
    Console.Write(s);
}

I read in this post that using + to concatenate strings is:

about 25% faster than using string.join()
about 15% faster than using string.format()
and about 10% faster than using a stringbuilder

Haven't actually tested it myself but this is some food for thought.

It is, so I tested it. My results:
Timer frequency in ticks per second = 2929775
Timer is accurate within 341 nanoseconds

Using + took 26,931,729 ticks
Using Join took 26,931,729 ticks
Using Concat took 27,652,841 ticks
Using Format took 164,192,838 ticks
Using Stringbuilder took 46,204,923 ticks

That's for 100,000,000 million string 'concats'. The difference between the fastest (+ and Join) and the slowest (Format) is 46.8 seconds. Stringbuilder is comparable to the fastest if you take the Clear of the object out of the timing loop.

Adding a comma will allow the use of distinct() (assuming certain namespaces are allowed):

using System;
using System.Linq;

namespace DW_411407_CS_CON
{
   class Program
   {
      static void Main(string[] args)
      {
         string str1 = "1,2,3";
         string str2 = "3,4,5";

         Console.WriteLine(string.Join(",",(str1+','+str2).Split(',').Distinct().ToArray()));
      }
   }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.