1

say i have two strings string str1 = "SAKVRLW" string str2 = "TCOEFO"

The result i wanted is below

result = "STACKOVERFLOW".

Sorry if this is a basic question but i am new to Linq ? Any help/suggestions to achieve this?

Anamay
  • 71
  • 1
  • 2
  • 10
  • 1
    Start with str1.Zip(str2). – Roger Lipscombe Jun 30 '15 at 15:42
  • 2
    Very difficult, very **useless**. – xanatos Jun 30 '15 at 15:46
  • Related: [Interleaved merge with LINQ](http://stackoverflow.com/questions/7224511/interleaved-merge-with-linq) – ryanyuyu Jun 30 '15 at 15:49
  • My guess (without trying it) would be to use the ```.Zip()``` LINQ method. – Martin Costello Jun 30 '15 at 15:49
  • 1
    `Zip` will get you some of the way, but this is complicated by your two strings being different lengths. You can get *STACKOVERFLO* quite easily. But, as @xanatos says, what's the point?! – Charles Mager Jun 30 '15 at 15:49
  • @RogerLipscombe Thanks..I got an idea – Anamay Jun 30 '15 at 15:54
  • 2
    Why restrict yourself to linq, linq is just shortcut methods for stuff you could do in a `foreach` or `for` loop. Solve your problem with a `for` loop first then come back and ask how would I turn this `for` loop in to linq (but first ask yourself do you really need to convert it at all?) – Scott Chamberlain Jun 30 '15 at 16:09
  • Sometime LINQ is elegant and sometime it is fast and sometimes it is both; sometimes is better quality (eg.g: when sorting). And sometimes it is none of the above.. – TaW Jun 30 '15 at 16:25
  • @ScottChamberlain Yes u are correct..I did it with foreach only, but was curious to know if i can achieve this using linq – Anamay Jun 30 '15 at 16:56
  • I stopped using `For` and `Foreach` loops roughly 5 years ago; really. I fully use Linq for processing lists of information. Why is that valuable? Because a majority of programming tasks are processing lists of data and the extension methods via the use of the dyanmic lambda expressions is so powerful. Don't let anyone dissuade you from learning a powerful tool. IMHO – ΩmegaMan Jul 01 '15 at 19:54

2 Answers2

1

Just for the fun of it:

string str1 = "SAKVRLW";
string str2 = "TCOEFO";

str1.ToCharArray()
    .Select((ch, ix)=> new { ch=ch, ix = ix*2 })
    .Concat(  str2.ToCharArray().Select((ch, ix)=> new { ch=ch, ix = ix*2 +1 }))
    .OrderBy(x=>x.ix) 
    .Select(x=>x.ch)
    .Aggregate("", (s,c)=>s+c).Dump();
ΩmegaMan
  • 29,542
  • 12
  • 100
  • 122
  • This works smooth..Thanks alot ... and this .Dump() is LinqPad extension method used for rendering object in console. – Anamay Jun 30 '15 at 17:04
  • I like your `Aggregate`...but I used the index into the other array in my answer. – ΩmegaMan Jun 30 '15 at 18:25
0

At the heart of this linq query is getting the index number from the select statement to use as an indexer into the other array.

Then its a matter of checking for an out of bounds condition while processing the char arrays into strings to be joined.

var str1 = "SAKVRLW";
var str2 = "TCOEFO";

var array2 = str2.ToCharArray();

string.Join(string.Empty, 
            str1.ToCharArray()
                .Select ((ch, index) => string.Format("{0}{1}", 
                                                       ch, 
                                                       index < array2.Length 
                                                       ? array2[index].ToString() 
                                                       : string.Empty  
                                                     )
                        )
           )

Outputs: STACKOVERFLOW


Using the above Select (projection) with an Aggregate instead of string.Join can be done as such

str1.ToCharArray()
    .Select ((ch, index) => string.Format("{0}{1}", ch, index < array2.Length ? array2[index].ToString() : string.Empty  ))
    .Aggregate ((seed, sentance) => seed + sentance )

Outputs: STACKOVERFLOW

ΩmegaMan
  • 29,542
  • 12
  • 100
  • 122