Re: Newbie String Questions



In article <456cb38d$0$8759$ed2619ec@xxxxxxxxxxxxxxxxxxxxxxxxxx>,
Wesley Hall <noreply@xxxxxxxxxxx> wrote:

Dom wrote:

[ snip ]

2. Why is there a concat method? Is there any difference between
these statements:
a) -- s = s.concat ("ABC");
b) -- s = s + "ABC";

Functionally, there is no difference, although there are probably some
subtle things related to the above. I can't be bothered to think it
through right now :oP

I agree that there's no functional difference, but it may be better to
use the concat method rather than the + operator.

The Javadocs for String indicate that the language has special support
for the concatenation operator (+) and indicates how it works. In the
source, you find that concat does a check for a zero-length parameter,
resulting in returning the original reference and thus no change to the
heap. But if a non-empty parameter is passed, it builds an array of
char to hold the old and new strings, then returns the result of calling
a String constructor with that array. Which is actually better probably
depends on a lot of factors, so...let's just say that the sure fire best
thing is not to do either in large quantities, preferring instead to use
a StringBuilder or StringBuffer.

= Steve =
--
Steve W. Jackson
Montgomery, Alabama
.



Relevant Pages

  • Re: Shuffle an array of integers.
    ... > I changed the type of Ballenbak array from int to String. ... I'm reasonably sure that the shuffle probably relies on the ... Steve W. Jackson ...
    (comp.lang.java.programmer)
  • RE: How to create arrays of arrays?
    ... > Hey Perlers! ... > "string 2b". ... > How do explicitly assign an array (not an array reference, ... > sub concat { ...
    (perl.beginners)
  • Re: problem evaluating if { ! [file exists $DIRVAL]} inside script
    ... I wrote the following code in a small script and verified it worked. ... A good rule of thumb is to never use concat except on lists. ... portion of my code used to take a directory string and create the ... The first part (currpath1) concats $currpath with "/". ...
    (comp.lang.tcl)
  • Re: merge JSON structures
    ... String objects, I've shown an Array concat, adding a String concat ... Overwrite arg2 if they exist in both. ...
    (comp.lang.javascript)
  • Re: c# interview question
    ... the int, b) call ToString on both the string and the now boxed int. ... Concat is a red herring. ...
    (microsoft.public.dotnet.languages.csharp)