|
-
Mar 20th, 2006, 06:13 PM
#1
Thread Starter
Admodistrator
[RESOLVED] String.Join with an arraylist?
Okay I have an arraylist and I want to output it as one single string into a text file. The trouble is string.join will not let me use the arraylist as an array, and arraylist.toarray doesnt work either. Does anyone have any suggestions? Do I have to use stringbuilder?
-
Mar 20th, 2006, 06:18 PM
#2
Re: String.Join with an arraylist?
String.Join should work fine.
VB Code:
Dim MyString As String = String.Join(Nothing, MyArrayList.ToArray())
-
Mar 20th, 2006, 06:35 PM
#3
Re: String.Join with an arraylist?
I dont think that will work, because Arraylist is of type "object", and the join needs a string array....
I usually just loop all items concatenating them...
-
Mar 20th, 2006, 06:36 PM
#4
Thread Starter
Admodistrator
Re: String.Join with an arraylist?
Thats exactly what I tried and reported that it does not work..
-
Mar 20th, 2006, 06:40 PM
#5
Re: String.Join with an arraylist?
VB Code:
Dim myString As String = String.Join(String.Empty, DirectCast(myArrayList.ToArray(GetType(String)), String()))
-
Mar 20th, 2006, 06:41 PM
#6
Re: String.Join with an arraylist?
If you dont want to loop all items, you can use the copyto method to copy to a temp string array, then use string.join on it, like the example below:
VB Code:
Dim MyList As New ArrayList
MyList.Add("this")
MyList.Add("that")
Dim MyStrings(MyList.Count - 1) As String
MyList.CopyTo(MyStrings)
Dim MyString As String = String.Join(",", MyStrings)
MessageBox.Show(MyString)
P.S. Ok JM.. I knew there was a better way, and that is what I was trying to do... but unsuccessfully I totally missed the part about specifying the type in the ToArray method...
Last edited by gigemboy; Mar 20th, 2006 at 06:44 PM.
-
Mar 20th, 2006, 07:00 PM
#7
Thread Starter
Admodistrator
Re: String.Join with an arraylist?
Thanks, just wondering, is it bad practice to use it like this?
CType(MySettings.ToArray(GetType(String)), String()))
I typed it after reading yours, and it works so I'm wondering if it is bad?
-
Mar 20th, 2006, 07:07 PM
#8
Re: String.Join with an arraylist?
 Originally Posted by |2eM!x
Thanks, just wondering, is it bad practice to use it like this?
CType(MySettings.ToArray(GetType(String)), String()))
I typed it after reading yours, and it works so I'm wondering if it is bad?
It's not bad but DirectCast is more efficient. If you know for sure that the object you're casting IS the type that you're casting as then you should use DirectCast as it does not do any type-checking. If the run-time types are not or may not be the same then you should use CType, as it will do some additional type checking and perfrom a conversion if one is required and exists. This means that in the vast majority of cases you should be using DirectCast.
-
Mar 20th, 2006, 07:11 PM
#9
Thread Starter
Admodistrator
Re: String.Join with an arraylist?
Okay, thanks alot
-
Mar 20th, 2006, 10:07 PM
#10
Re: [RESOLVED] String.Join with an arraylist?
I was benchmarking the code for the three different methods given, JM's suggestion, my example with CopyTo, and then just looping all items and concatenating them, just to see what the performance differences were. The fastest seemed to be using the .CopyTo method, although just looping the items and concatenating them wasnt far behind at all, both around 78 milliseconds (although concatenating them sometimes jumped to 90+ milliseconds for some reason on some loops). JM' ssuggestion came in between 109ms and 125ms...
If anyone wants me to post the benchmark code, Id be glad too. The example tested was a very short list of items in an arraylist... not tested on a large list, however...
***EDIT - with 100 items in the list, JM's code edged out copyto (1170ms vs 1230ms), and the slowest, was by far looping and concatenating, at 7300ms...
Last edited by gigemboy; Mar 20th, 2006 at 10:14 PM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|