From the February 2016 Issue of Prestwood eMag
 
Prism Language Basics:
Delphi Prism String Concatenation (+)
 
Posted 2/17/2009 on 2/17/2009 and updated 9/15/2009
Prism Code Snippet:

KB101921

Delphi Prism String Concatenation

Unlike Delphi, Prism performs implicit casting. To concatenate two strings, a string to an integer, or a string to a floating point number, use the + operator. For example, to convert a floating point number to a string just concatenate an empty string to the number as in "" + 3.2.

Alternatively, you can use the System.Text.StringBuilder class which frequently but not always provides faster code.

Syntax Example:
var FirstName : String;
var LastName : String;
  
FirstName := 'Mike';
LastName := 'Prestwood';
ShowMessage('Full name: ' + FirstName + ' ' + LastName);
  
//Implicit casting of numbers.
//
//This fails:
//MessageBox.Show(3.3);
//
//This works:
MessageBox.Show("" + 3.3);

Using System.Text.StringBuilder

Many times using the StringBuilder class is faster than concatenation. If you have extensive string manipulations to perform, consider using the StringBuilder class.

First add System.Text to your uses:

Uses
  System.Text;

Then use the methods from the StringBuilder class. For example, you can use Append and ToString as follows:

method MainForm.StringBuilderBtn_Click(sender: System.Object; e: System.EventArgs);
var
FirstName : String;
LastName : String;
sbFullName := new StringBuilder;
begin
FirstName := 'Mike';
LastName := 'Prestwood';

//Concatenation.
MessageBox.Show(FirstName + " " + LastName);
 
  //StringBuilder class.
sbFullName.Append(FirstName);
sbFullName.Append(" ");
sbFullName.Append(LastName);
  MessageBox.Show(sbFullName.ToString);
end;


Comments

1 Comments.
Share a thought or comment...
Comment 1 of 3

FWIW, there's a couple of Catcatenations in with the Concatentations ;-)

Posted 6/29/2011

Comment 2 of 3

Oops! Embarassed The spelling errors are fixed now. Thanks for letting me know.

Posted 7/1/2011

Comment 3 of 3

Now that I have done more .net and Delphi Prism, I also wouldn't do a concatenation just to convert to string.

You can actually do:

MessageBox.Show( 3.3.ToString );

Posted 1/6/2012
Code Contributed By Mike Prestwood:

Mike Prestwood is a drummer, an author, and creator of the PrestwoodBoards online community. He is the President & CEO of Prestwood IT Solutions. Prestwood IT provides Coding, Website, and Computer Tech services. Mike has authored 6 computer books and over 1,200 articles. As a drummer, he maintains play-drums.com and has authored 3 drum books. If you have a project you wish to discuss with Mike, you can send him a private message through his PrestwoodBoards home page or call him 9AM to 4PM PST at 916-726-5675 x205.

Visit Profile
www.prestwood.com For service: 916-726-5675
or support@prestwood.com
Copyright (C) Prestwood IT Solutions.
All Rights Reserved.
Printed 9/30/2020