How to concatenate strings in Visual C++ .NET or in Visual C++ 2005 (311580)



The information in this article applies to:

  • Microsoft Visual C++ 2005 Express Edition
  • Microsoft Visual C++ .NET (2003)
  • Microsoft Visual C++ .NET (2002)

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

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:
  1. Click Project, and then click <ProjectName> Properties.

    Note <ProjectName> is a placeholder for the name of the project.
  2. Expand Configuration Properties, and then click General.
  3. 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

Modification Type:MajorLast Reviewed:12/31/2005
Keywords:kbHOWTOmaster KB311580 kbAudDeveloper