1

So I have a string array called page and a string array called notesSplit which is created using notes.split().

notessplit could have a variable number of newlines however is never over 10 lines.

I'd like to overwrite the contents of "page" from index 20 - 30 leaving blank lines if the index does not exist in notessplit.

Any ideas?

var page = new string[44]; <-- actually this is from a text file
string notes = "blah \n blah \n";    
string[] notesSplit = notes.Split(new string[] { Environment.NewLine }, StringSplitOptions.None);

What I have initially come up with is:

for (var i = 0; i < 9; i++) 
{ 
  if (notesSplit[i] != null) 
  { 
    Page[i + 20] = notesSplit[i]; 
  } else { 
    Page[i + 20] = System.Environment.NewLine; 
  } 
}
VulgarBinary
  • 3,520
  • 4
  • 20
  • 54
Johnny Grimes
  • 411
  • 7
  • 17
  • Please explain more clearly, show example input and desired output – Sami Kuhmonen May 14 '15 at 05:49
  • What do you mean by *"index does not exist"*? Do you mean it is out of bounds of the array? Or there is no content? – Yuval Itzchakov May 14 '15 at 05:49
  • possible duplicate of [Merging two arrays in .Net](http://stackoverflow.com/questions/59217/merging-two-arrays-in-net) – Zohar Peled May 14 '15 at 05:52
  • @ZoharPeled Why do you think he wants to merge the arrays? – Yuval Itzchakov May 14 '15 at 05:53
  • *"I'd like to overwrite the contents of "page" from index 20 - 30 leaving blank lines if the index does not exist in notessplit."* Seems to me like merging the notesSplit array into the notesArray – Zohar Peled May 14 '15 at 05:54
  • @ZoharPeled He doesn't want to merge the entire array. He wants to replace elements at certain indexes. – Yuval Itzchakov May 14 '15 at 05:55
  • @YuvalItzchakov: Did you see the accepted answer on that post? using Array.Copy seems to be exactly what he needs, along with the resize of the SplitNotes array when neccesary – Zohar Peled May 14 '15 at 06:00
  • @ZoharPeled The accepted answer does a re-size and a copy. Why would he need any of those? – Yuval Itzchakov May 14 '15 at 06:02
  • @YuvalItzchakov Check the accepted answer as well as my suggestion. To my understanding, they result in the same thing. Note that my suggestion is exactly the same as the accepted answer in the post I've marked as duplicate. – Zohar Peled May 14 '15 at 06:10

2 Answers2

3

I'm pretty sure this is what you're looking for.

public string[] Replace(string[] page, string[] notes, int start, int length)
{
  for(var i = 0; i + start < page.Length && i < length; i++)
  {
    if(notes != null && notes.Length > (i))
      page[i+start] = notes[i];
    else
      page[i+start] = Enviroment.NewLine;
  }

  return page;
}
VulgarBinary
  • 3,520
  • 4
  • 20
  • 54
  • Thanks, I came up with this, but yours is nicer. for (var i = 0; i < 9; i++) { if (notesSplit[i] != null) { Page[i + 20] = notesSplit[i]; } else { Page[i + 20] = System.Environment.NewLine; } } – Johnny Grimes May 14 '15 at 05:58
  • In this example you would call: `Replace(page, notesSplit, 20, 8);` given the example you posted in a comment. – VulgarBinary May 14 '15 at 05:58
  • I modified the solution to work with your sample where `notesSplit[i]` instead of `i+start` – VulgarBinary May 14 '15 at 06:02
  • @VulgarBinary `page` is a `string[]`, you can use the `Length` property instead of the `Count()` extension method. – Yuval Itzchakov May 14 '15 at 06:04
  • @YuvalItzchakov - `IEnum` extensions account for Length when it's available. :-) O(1) on Array verse O(n) on a true enumerable. I can change it if it would make you happier but it doesn't impact the performance, it's just 1 less character to type Length. – VulgarBinary May 14 '15 at 06:07
  • @VulgarBinary [No, it doesn't](http://referencesource.microsoft.com/#System.Core/System/Linq/Enumerable.cs,41ef9e39e54d0d0b). It looks for `Count` on an `ICollection` or `ICollection`. – Yuval Itzchakov May 14 '15 at 06:09
2

Another option, instead of looping over the arrays, is to use Array.Resize method and Array.Copy method:

// Copied your array definiton:
var page = new string[44];
string notes = "blah \n blah \n";
string[] notesSplit = notes.Split(new string[] { Environment.NewLine }, StringSplitOptions.None);

// The suggested solution:
if (notesSplit.Length < 10) 
{    
    Array.Resize(ref notesSplit, 10);
}
Array.Copy(notesSplit, 0, page, 20, 10);

Additional information on Array.Copy can be found here on MSDN

Zohar Peled
  • 79,642
  • 10
  • 69
  • 121
  • @VulgarBinary: This is the exact definition of code readability... as to performance I didn't test, but the outcome should be the same. – Zohar Peled May 14 '15 at 06:09
  • My syntax amounts to `Replace(page, notesSplit, 20, 10)` Yours is much longer ;-) It's the reason I made it it's own method. – VulgarBinary May 14 '15 at 06:10
  • Regardless, both are valid and both should work. Just razzing you :-) – VulgarBinary May 14 '15 at 06:13
  • @VulgarBinary: Wrapping code inside a method to make it reusable is blessed. However, I could have done the same thing and my method would have, as you wrote yourself, " Prettier syntax but exact same performance". – Zohar Peled May 14 '15 at 06:13
  • 1
    @VulgarBinary: I agree. they are both valid. – Zohar Peled May 14 '15 at 06:15
  • 1
    Anytime :-) When am involved in questions any thing worth linking I generally do on any answers worth reading or inside the question itself. Helps others stumbling across the Q if they are uncertain about syntax, concepts, or technologies. Happy to help! – VulgarBinary May 14 '15 at 06:25
  • Actually, I usually add links like my last update, but since the OP have already accepted your answer, I've skipped that part... In fact, the only reason I've bothered to write it is to show that this question is in fact a duplicate... – Zohar Peled May 14 '15 at 06:29
  • -deep breath- Read this :-) http://blog.stackoverflow.com/2010/11/dr-strangedupe-or-how-i-learned-to-stop-worrying-and-love-duplication/ – VulgarBinary May 14 '15 at 06:31
  • @VulgarBinary Now that's a link worth reading! :-). Fully Accepted. Thanks man! – Zohar Peled May 14 '15 at 06:35
  • @ZoharPeled I'm not sure that this solution accounts for the newline characters required on each empty line. – Johnny Grimes May 14 '15 at 22:49
  • You are correct, I've missed it. I'll try to find a way to add them without an explicit loop. – Zohar Peled May 15 '15 at 05:18