Microsoft KB Archive/311580

= How to concatenate strings in Visual C++ .NET or in Visual C++ 2005 =

Article ID: 311580

Article Last Modified on 12/31/2005

-

APPLIES TO


 * Microsoft Visual C++ 2005 Express Edition
 * Microsoft Visual C++ .NET 2003 Standard Edition
 * Microsoft Visual C++ .NET 2002 Standard Edition

-



This article was previously published under Q311580



This article refers to the following Microsoft .NET Framework Class Library namespace:
 * System.String

IN THIS TASK
SUMMARY
 * String::Concat Method
 * Complete Code Sample



SUMMARY
This article explains how to concatenate strings by using Managed Extensions to Visual C++ .NET or Visual C++ 2005.

back to the top

String::Concat Method
You cannot use the + operator to concatenate strings in Managed Extensions to Visual C++ .NET or Visual C++ 2005. However, you can use the String::Concat static method to concatenate strings, as in the following sample code: System::String* str1= System::String::Concat( S&quot;Hello&quot;, S&quot;World&quot;); back to the top

Complete Code Sample
//compiler options : cl /clr using namespace System; int main{ String *s1= S&quot;World&quot;; //this technique only works for up to 4 objects. String *s2= String::Concat(S&quot;Hello &quot;,s1 ,S&quot; from Managed C++!&quot;); Console::WriteLine(s2); //If you have more strings than that, you can do it with a string array: String* sa[] = { S&quot;one&quot;, S&quot;two&quot;, S&quot;three&quot;, S&quot;four&quot;, S&quot;five&quot;, S&quot;six&quot;, S&quot;seven&quot;, S&quot;eight&quot; }; String *s = String::Concat(sa); Console::WriteLine(s); return 0; } Note You must add the common language runtime support compiler option (/clr:oldSyntax) in Visual C++ 2005 to successfully compile the previous code sample. To add the common language runtime support compiler option in Visual C++ 2005, follow these steps:
 * 1) using 
 * 1) Click Project, and then click   Properties.

Note  is a placeholder for the name of the project.
 * 1) Expand Configuration Properties, and then click General.
 * 2) Click to select Common Language Runtime Support, Old Syntax (/clr:oldSyntax) in the Common Language Runtime support project setting in the right pane, click Apply, and then click OK.

For more information about the common language runtime support compiler option, visit the following Microsoft Web site:

/clr (Common Language Runtime Compilation)

http://msdn2.microsoft.com/en-us/library/k8d11d4s.aspx

back to the top

Keywords: kbhowtomaster KB311580

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.