954,529 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

union of two strings A and B

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?

juce
Newbie Poster
13 posts since Dec 2011
Reputation Points: 10
Solved Threads: 0
 

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);

ChrisHunter
Posting Whiz in Training
276 posts since Feb 2011
Reputation Points: 36
Solved Threads: 28
 

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()

skatamatic
Posting Shark
959 posts since Nov 2007
Reputation Points: 403
Solved Threads: 129
 
gusano79
Posting Pro
521 posts since May 2004
Reputation Points: 182
Solved Threads: 77
 
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.

skatamatic
Posting Shark
959 posts since Nov 2007
Reputation Points: 403
Solved Threads: 129
 

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);
}
Momerath
Nearly a Senior Poster
3,386 posts since Aug 2010
Reputation Points: 1,232
Solved Threads: 558
 

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:[INDENT]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
[/INDENT]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.

Momerath
Nearly a Senior Poster
3,386 posts since Aug 2010
Reputation Points: 1,232
Solved Threads: 558
 

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()));
      }
   }
}
thines01
Postaholic
Team Colleague
2,424 posts since Oct 2009
Reputation Points: 445
Solved Threads: 402
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: