[Newbies] [Q] How should one concatenate Strings?

Ralph Johnson johnson at cs.uiuc.edu
Sun May 7 11:09:14 UTC 2006


On 5/7/06, David T. Lewis <lewis at mail.msen.com> wrote:
> The comma character is actually is actually a message that is understood
> by strings. The message is implemented in class Collection (in the
> "copying" method category), and since strings are types of collections,
> it works for strings also.

Hey, Newbies!  In case you missed the implications of what Dave said,
you can concatenate arrays, ordered collections, and bit arrays, as
well.  Once you learn how to concatenate strings, you can use that
knowledge all over.

Also, some languages use + for concatenation, and I have always liked
the fact that Smalltalk uses , because + ought to be commutitive.

If you are building very long strings by using , in an inner loop, it
can be slow because you will have an N squared algorithm.  You can
convert that into a linear time algorithm by writing to an WriteStream
using nextPut: and nextPutAll: and then asking the WriteStream for its
contents.  It leads to a little more complicated code, but on some
programs in can create a dramatic performance improvement.  In
general, though, use "comma", since it is simpler.

-Ralph Johnson


More information about the Beginners mailing list