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"Hello", S"World");
back to the top
Complete Code Sample
//compiler options : cl /clr
#using <mscorlib.dll>
using namespace System;
int main(){
String *s1= S"World";
//this technique only works for up to 4 objects.
String *s2= String::Concat(S"Hello ",s1 ,S" from Managed C++!");
Console::WriteLine(s2);
//If you have more strings than that, you can do it with a string array:
String* sa[] = { S"one", S"two", S"three", S"four", S"five", S"six", S"seven", S"eight" };
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:
- Click Project, and then click <ProjectName> Properties.
Note <ProjectName> is a placeholder for the name of the project. - Expand Configuration Properties, and then click General.
- 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:
back to the top