PC Review


Reply
Thread Tools Rate Thread

Concatenate Arrays?

 
 
Axel Dahmen
Guest
Posts: n/a
 
      30th Nov 2004
Hi,

I want to concatenate two string arrays. Is it possible to concatenate them
gracefully, i.e. without copying each element manually?

TIA,
Axel Dahmen

PS.: Here's an example of what I am about:

string[] lst1,lst2,ret;
lst1=Directory.GetFiles("C:\Temp\*.jpg");
lst2=Directory.GetFiles("C:\Temp\*.gif");

ret=lst1+lst2; // ????


 
Reply With Quote
 
 
 
 
Jon Skeet [C# MVP]
Guest
Posts: n/a
 
      30th Nov 2004
Axel Dahmen <NO_SPAM@No_Spammer_Knows.invalid> wrote:
> I want to concatenate two string arrays. Is it possible to concatenate them
> gracefully, i.e. without copying each element manually?


It depends on exactly what you mean by "manually". Array.Copy and
Array.CopyTo will probably help you, but obviously you still need to do
the copy *somehow*.

--
Jon Skeet - <(E-Mail Removed)>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
 
Reply With Quote
 
Robby
Guest
Posts: n/a
 
      30th Nov 2004

Declare a new String array with the correct number of elements. Then use
the Copy method of Array to copy the two arrays into the new array. This
takes 3 lines of code but should be faster than doing two loops. Note that
this performs a shallow copy.

Robby


"Axel Dahmen" <NO_SPAM@No_Spammer_Knows.invalid> wrote in message
news:%23B$(E-Mail Removed)...
> Hi,
>
> I want to concatenate two string arrays. Is it possible to concatenate
> them
> gracefully, i.e. without copying each element manually?
>
> TIA,
> Axel Dahmen
>
> PS.: Here's an example of what I am about:
>
> string[] lst1,lst2,ret;
> lst1=Directory.GetFiles("C:\Temp\*.jpg");
> lst2=Directory.GetFiles("C:\Temp\*.gif");
>
> ret=lst1+lst2; // ????
>
>



 
Reply With Quote
 
Stu Smith
Guest
Posts: n/a
 
      30th Nov 2004

"Axel Dahmen" <NO_SPAM@No_Spammer_Knows.invalid> wrote in message
news:%23B$(E-Mail Removed)...
> Hi,
>
> I want to concatenate two string arrays. Is it possible to concatenate

them
> gracefully, i.e. without copying each element manually?


I have a set of array utilities, one of which is:

public static System.Array Add( System.Array first, System.Array second )
{
Type type = first.GetType().GetElementType();
System.Array result = System.Array.CreateInstance( type, first.Length +
second.Length );

first.CopyTo( result, 0 );
second.CopyTo( result, first.Length );

return result;
}

Obviously you'll have to cast the return value.

Stu

>
> TIA,
> Axel Dahmen
>
> PS.: Here's an example of what I am about:
>
> string[] lst1,lst2,ret;
> lst1=Directory.GetFiles("C:\Temp\*.jpg");
> lst2=Directory.GetFiles("C:\Temp\*.gif");
>
> ret=lst1+lst2; // ????
>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
concatenate with arrays hande Microsoft Excel Worksheet Functions 4 3rd Aug 2009 03:35 AM
Jagged Arrays Problem - How to Assign Arrays to an Array Zigs Microsoft Excel Programming 3 11th Apr 2007 01:39 AM
Concatenate / Select Subset of Arrays =?Utf-8?B?QW5kcmV3IEhhbGwgTlo=?= Microsoft Excel Programming 2 17th Mar 2007 01:51 PM
Arrays - declaration, adding values to arrays and calculation Maxi Microsoft Excel Programming 1 17th Aug 2006 04:13 PM
Concatenate Byte[] arrays Marius Cabas Microsoft C# .NET 4 12th Nov 2004 11:20 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 06:23 AM.