473,394 Members | 1,840 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Copy string array to string

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
Nov 21 '05 #1
12 4883
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
"DumberThanSnot" <Du************@nospam.com> wrote in message
news:uy**************@TK2MSFTNGP12.phx.gbl...
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

Nov 21 '05 #2
"DumberThanSnot" <Du************@nospam.com> schrieb:
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()
///

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

Nov 21 '05 #3

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
On Thu, 3 Feb 2005 07:15:00 -0800, "DumberThanSnot"
<Du************@nospam.com> wrote:
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

Nov 21 '05 #4
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

"DumberThanSnot" wrote:
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

Nov 21 '05 #5
Addendum:

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

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

Nov 21 '05 #6
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
Nov 21 '05 #7
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
Nov 21 '05 #8
Why do you think join is better? Any hard facts supporting either way?

Chris

"Cor Ligthert" <no************@planet.nl> wrote in message
news:uB*************@TK2MSFTNGP09.phx.gbl...
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

Nov 21 '05 #9
Do take a look of GetEnumerator() that might help.

chanmm

"DumberThanSnot" <Du************@nospam.com> wrote in message
news:uy**************@TK2MSFTNGP12.phx.gbl...
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

Nov 21 '05 #10
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


Nov 21 '05 #11
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
On Thu, 3 Feb 2005 17:11:08 +0100, "Cor Ligthert"
<no************@planet.nl> wrote:
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


Nov 21 '05 #12
"Chris, Master of All Things Insignificant" <chris@No_Spam_Please.com>
schrieb:
Why do you think join is better? Any hard facts supporting either way?


I *assume* its implementation is "optimized", because its not implemented in
the 'String' class directly:

<URL:http://www.123aspx.com/Rotor/RotorSrc.aspx?rot=42059>

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

Nov 21 '05 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

30
by: franky.backeljauw | last post by:
Hello, I am wondering which of these two methods is the fastest: std::copy, which is included in the standard library, or a manually written pointer copy? Do any of you have any experience with...
1
by: Matt Garman | last post by:
What is the "best" way to copy a vector of strings to an array of character strings? By "best", I mean most elegantly/tersely written, but without any sacrifice in performance. I'm writing an...
4
by: Venkat | last post by:
Hi All, I need to copy strings from a single dimensional array to a double dimensional array. Here is my program. #include <stdio.h> #include <stdlib.h>
6
by: Karl Ebener | last post by:
Hi! I am currently using a string to hold data (can be strings as well as binary!). Now it occured to me, that the <string> might be unable to handle Null-Bytes. Is that so? Following...
4
by: Simon Schaap | last post by:
Hello, I have encountered a strange problem and I hope you can help me to understand it. What I want to do is to pass an array of chars to a function that will split it up (on every location where...
15
by: Kueishiong Tu | last post by:
How do I copy the content of a string in one encoding (in my case big5) to a char array (unmanaged) of the same encoding? I try the following String line = S"123æ°´æ³¥"; char buffer; ...
3
by: marfi95 | last post by:
Hi all. I need to copy a byte array into a string, but starting at a specific location in the byte array. This is where I get hung up. For example if my byte array is (100) big, I might want to...
17
by: Chad | last post by:
I'm want static char *output; to hold the modified string "tel chad" However, when I debug it, static char *output holds the ascii value of the strng, and not the string itself. Here is...
1
by: illegal.prime | last post by:
I would like to have an array of objects (whose class I define) and then just invoke either: MyClass clonedArray = (MyClass) myArray.Clone(); OR Array.Copy(myArray, clonedArray, myArray.Length);...
5
by: tshad | last post by:
How do you easily make a copy of an arraylist? If you do: arrayList2 = arrayList1 You get a pointer so that if you clear arrayList2 (arrayList2.Clear) - arrayList1 is also cleared. I...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.