IT SOLUTIONS
Your full service technology partner! 
-Collapse +Expand
PHP
Search PHP Group:

Advanced
-Collapse +Expand PHP Store
PRESTWOODSTORE
   ► KBTo/From GuidesReferenceOperators  Print This    All Groups  

String Concatenation (Cross Ref > Operators)

String Concatenation

Access VBA:  "String Concatenation" & or +

Although you can use either a & or a + to concatenate values, my preference is to use a + because more languages use it. However, if you use & then some type conversions are done for you. If you use + you will sometimes have to cast a value to concatenate it. For example, you will have to use CStr to cast a number to a string if you use the + operator as a concatenation operator.

Syntax Example:
Dim FirstName As String
Dim LastName As String
 
FirstName = "Mike"
LastName = "Prestwood"
 
MsgBox "Full name: " & FirstName & " " & LastName
 
MsgBox "2+2=" + CStr(2+2)

More Info

ASP Classic:  "String Concatenation" & or +

Although you can use either a & or a + to concatenate values, my preference is to use a + because more languages use it. However, if you use & then some type conversions are done for you. If you use + you will sometimes have to cast a value to concatenate it. For example, you will have to use CStr to cast a number to a string if you use the + operator as a concatenation operator.

Syntax Example:
Dim FirstName
Dim LastName
 
FirstName  = "Mike"
LastName  = "Prestwood"
 
Response.Write "Full name: " & FirstName & " " + LastName
 
Response.Write "2+2=" + CStr(2+2)

More Info

C#:  "String Concatenation" +

C# performs implicit casting of numbers to strings. 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:
String FirstName;
String LastName;
Int16 Age;
FirstName = "Mike";
LastName = "Prestwood";
Age = 43;
Console.WriteLine(FirstName + " " + LastName + " is " + Age + ".");
  
//Implicit casting of numbers.
//
//This fails:
//MessageBox.Show(3.3);
//
//This works:
MessageBox.Show("" + 3.3); 


More Info

C++:  "String Concatenation" + or append

The + operator can be used with any combination of C++ strings, C strings and characters.

Syntax Example:
string fullname;

fullname = "Mike ";
fullname.append("Prestwood");

cout << "Hello " + fullname + "." << endl;

More Info

C++/CLI:  "String Concatenation" +

C++/CLI performs implicit casting of numbers to strings. 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:
//Implicit casting of numbers.
//
//This fails:
//MessageBox::Show(3.3);
//
//This works:
MessageBox::Show("" + 3.3);


Linked Certification Question(s)

The following are practice certification questions with answers highlighted. These questions were prepared by Mike Prestwood and are intended to stress an important aspect of this KB post. All our practice questions are intended to prepare you generally for passing any certification test as well as prepare you for professional work.

Beginner

1 Beginner Level Question

Question #1: True or False?

C++/CLI supports implicit casting which allows you to use the + operator to concatenate strings and numbers as in the following line of code:

MessageBox::Show("I am " + 42 + " years old.");
Answer:
  • True
  • False
  • More Info

     

    Corel Paradox:  "String Concatenation" +

    String literals s are limited to 255 characters but you can simply add two strings together as in:

    s = "A long string." + "Another long string."
    Syntax Example:
    var
    FirstName  String
      LastName  String
    endVar
     
    FirstName  = "Mike"
    LastName  = "Prestwood"
    msgInfo("", "Full name: " + FirstName + " " + LastName)

    More Info

    Delphi:  "String Concatenation" +

    Use the + operator to concatenate two strings. Use IntToStr to convert an integer to a string and FloatToStr to convert a floating point number to a string.

    Syntax Example:
    var 
      FirstName : String; 
      LastName : String;
    begin 
      FirstName := 'Mike'; 
      LastName := 'Prestwood';
      ShowMessage('Full name: ' + FirstName + ' ' + LastName);
      
      ShowMessage(FloatToStr(3.2));
    end;

    More Info

    Delphi Prism:   +

    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;

    More Info

    Code:  Delphi Prism String Concatenation (+)

    Java:  "String Concatenation" + or append

    In Java, you use either the String concatenation + operator or StringBulder class methods such as append. Since Java compilers frequently create intermediate objects when the + operator is used and don't when StringBuilder.append is used, the append method is faster than the + operator.

    In general, use the convenience of a + operator when speed is not an issue. For example, when concatenating a small number of items and when code isn't executed very frequently. A decent rule of thumb is to use the + operator for general purpose programming and then optimize the + operator with StringBuilder.append as needed.

    Syntax Example:

    Simple + operator example:

    System.out.println("Hello" + " " + "Mike.");

     

    Using StringBuilder example:

    StringBuilder myMsg = new StringBuilder();

    myMsg.append("Hello ");
    myMsg.append("Mike.");
     
    System.out.println(myMsg);


    More Info

    JavaScript:  "String Concatenation" +

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

    Syntax Example:
     
    // -->

    More Info

    Perl:  "String Concatenation" .

    Perl uses a period (.) known as a dot to concatenate strings.

    Syntax Example:
    $fname = "Mike";
    $lname = "Prestwood";

    $fullname = $fname . $lname . "
    ";

    print "My name is " . "Mike.
    ";


    More Info

    PHP:  "String Concatenation" .

    PHP uses a period (.) known as a dot to concatenate strings.

    Syntax Example:
    $fname = "Mike";
    $lname = "Prestwood";

    $fullname = $fname . $lname . "
    ";

    echo "My name is " . "Mike.
    ";

    Here's a complete example that demonstrates a few string concepts:

    <?PHP
    //Now using quotes.
    $fname = "Mike";
    $lname = "Prestwood";

    $fullname = $fname . $lname;

    echo $fullname . '<br>';

    //Two literals too:
    echo "My name is " . "Mike.<br>";

    //Long strings.
    $MyMsg = "This is a long string and
    unlike some other languages. PHP allows
    you to put strings on multiple lines
    like this.";

    echo $MyMsg;
    ?>

    More Info

    Code:  PHP String Concatenation (.)

    VB Classic:  "String Concatenation" & or +

    Although you can use either a & or a + to concatenate values, my preference is to use a + because more languages use it. However, if you use & then some type conversions are done for you. If you use + you will sometimes have to cast a value to concatenate it. For example, you will have to use CStr to cast a number to a string if you use the + operator as a concatenation operator.

    Syntax Example:
    Dim FirstName As String
    Dim LastName As String
     
    FirstName = "Mike"
    LastName = "Prestwood"
     
    MsgBox "Full name: " & FirstName & " " + LastName
     
    MsgBox "2+2=" + CStr(2+2)

    More Info

    VB.Net:  "String Concatenation" + or &

    To concatenate two strings, use either the + or & operators. The & operator implicitly converts numbers. If you use the + operator to concatenate a string and a number, you have to cast the number as a string with CStr.

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

    Syntax Example:
    Dim FullName
    Dim Age
      
    //You can use + for strings.
    FullName = "Prestwood"
    Console.WriteLine("Hello " + FullName)
     
    //For implicit casting, use &
    Age = 35
    Console.WriteLine(FullName & " is " & Age & " years old.")
    'Implicit casting of numbers.
    '
    'This works:
    MessageBox.Show(3.3)
      
    'This fails:
    'MessageBox.Show("" + 3.3)
      
    'This works:
    MessageBox.Show("" + CStr(3.3))
      
    'Implicit casting &. This also works:
    MessageBox.Show("" & 3.3)

    More Info





    Go ahead!   Use Us! Call: 916-726-5675  Or visit our new sales site: 
    www.prestwood.com


    ©1995-2021 Prestwood IT Solutions.   [Security & Privacy]