Copy string array to string

D

DumberThanSnot

is there a faster way to copy an ArrayList of strings to a string other than
a tight loop.

In it's most simple terms, I'm currently using something like this...

----------------------------

*** NOTE: rtbXML is a Rich Text Box on the form

dim iCntr as integer
dim sText as string
dim arList as new ArrayList

' Code here will be building a very large Rich Text Box string.
arList.Add("String 1")
arList.Add("String 2")
arList.Add("String 3")

' I'd like to replace the following loop with something faster
sText = ""
for iCntr = 0 to arList.Count - 1
sText += arList.Item(iCntr)
next

rtbXML.Rtb = sText

-----------------------------

I'm building the string to hand off to a Rich Test Box such as "rtbXML.rtb =
stext". I'm originally adding the lines of RTB formats to the ArrayList
because adding the formats to the 'stext' variable in small chunks takes a
looooong time. The idea is to have a very fast memcpy to get the arraylist
to sText as quickly as possible. What I would really like is to go directly
from the arraylist to the rtbXML.Rtb, but I'll settle for a fast "memcpy".

Actually, what I would really like is to be able to display my XML text
files in a .NET browser box on my form. I was using the ActiveX browser
control to display the XML files. but, that meant that I had to install the
ActiveX dll for the browser on my customers computers. That wasn't so bad,
but to make a long story short, the environment that my customers have their
computers doesn't make it real easy to install all the extra drivers. So, I
decided to write my own XML viewer which works, but I need to speed up the
RTB section a bit.

thanks for any help,
Brian
 
C

Chris, Master of All Things Insignificant

I think using Stringbuilder class is suppose to be faster than just the
String class. I don't have evidence to back that up on me, just what I've
been reading in this newsgroups. Someone correct me if I haven't had enough
caffee yet this morning and I'm not thinking clearly.

Also, I was wondering if it would be faster just to assign the string
directly to the rtbXML.Rtb object in the loop. Two things I don't know
about it off the top of my head. When rtbXML.Rtb = sText runs does it have
to copy the string over to Rtb which makes two copies in memory, and if you
did rtbXML.Rtb += arList.Item(iCntr) if the rtbXML object does extra
processing on the object which would make it less cost effective.

Chris
 
H

Herfried K. Wagner [MVP]

DumberThanSnot said:
is there a faster way to copy an ArrayList of strings to a string other
than a tight loop.

Especially for composing large files/strings, I would use a
'System.Text.StringBuilder':

\\\
Imports System.Text
..
..
..
Dim sb As New StringBuilder()
For Each s As String In astr
sb.Append(s)
Next s
Me.RichTextBox1.Rtf = sb.ToString()
///
 
S

Samuel R. Neff

Dim al As New ArrayList
al.Add("One")
al.Add("Two")
al.Add("Three")

Dim s As String

s = String.Join(",", DirectCast(al.ToArray(GetType(String)),
String()))

BTW, when working with concatenating strings it's better to use the
StringBuilder class.

HTH,

Sam
 
G

Guest

Hi,

if one have to concat many strings it's the better way to use a
StringBuilder, it' much faster!!!

dim iCntr as integer
dim sText as string
dim arList as new ArrayList

arList.Add("String 1")
arList.Add("String 2")
arList.Add("String 3")

StringBuilder sbText = new StringBuilder()
for iCntr = 0 to arList.Count - 1
sbText.Append(arList.Item(iCntr))
next

rtbXML.Rtb = sbText.ToString()


Possibly if one have a Array of Strings ('String()') one can use String.Join()

Dim arString as String()

....

rtbXML.Rtb = String.Join("", arString)


but I don't now about performance!


Roland
 
H

Herfried K. Wagner [MVP]

Addendum:

If you have a string array, you can alternatively use 'Strings.Join' or
'String.Join' to concatenate the strings.
 
D

DumberThanSnot

thanks for all the replies... as per your ideas and examples... I replaced
the "string.join" method just to get the code running right now. I'm in the
process of converting all the applicable routines to stringbuilder.
unfortunately, I've been used to doing "memory work" in C++. I knew it
could be done faster in VB than I was doing, I just didn't quite know where
to start.

thanks again,
Brian
 
C

Cor Ligthert

Samuel,

I find your answer confusing (however read further)

You give as only one direct the exact right answer (I surely would have
given the stringbuilder answer as well, while I know this Join) and than
you can (mis) read from your message that the stringbuilder is better.

What it is in my opinion not the case. Your join answer is in my opinion the
best.

(As well meant to the OP of course)

Cor
 
C

Chris, Master of All Things Insignificant

Why do you think join is better? Any hard facts supporting either way?

Chris
 
C

Cor Ligthert

Chris,

Only because that it shows what you do in one statement.

I am not interested in the performance because that is probably about
nanoseconds in this case.

I am extremly propaganding however forever the stringbuilder in this
newsgroup and when you would search for it than you would see, that I have
had very hard discussions about that.

:)

Cor
 
S

Samuel R. Neff

Cor,

You're right, I wasn't very clear.

What I meant was that the code sample provided using String.Join was
the best solution to the stated question--how to join an ArrayList of
strings in a single line.

The comment about StringBuilder was a secondary comment to the code
from the OP where he had used a loop with standard old string
concatenation to perform the same operation. My intention here was to
state that StringBuilder was better than old style string
concatenation, but not that it was better than String.Join.

The quick answer is not always best if it's not explained--your point
is taken.

Thanks,

Sam
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top