Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Managed C++/CLI
  4. Converting and Concatenating Strings

Converting and Concatenating Strings

Scheduled Pinned Locked Moved Managed C++/CLI
c++csharptutorialquestion
2 Posts 2 Posters 3 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    KBL
    wrote on last edited by
    #1

    is for Managed Visual C++.NET.. Suppose I have two Strings. String1 = C://files String2 = data.txt I want to concatenate the two strings while inserting a character between them, to get a new string: C://file/data.txt Here the character is an extra "/". Once this is done, I would like to convert the new string to a char[20]. Does anyone know how to perform this task? I would be very grateful.. Thanks KBL

    J 1 Reply Last reply
    0
    • K KBL

      is for Managed Visual C++.NET.. Suppose I have two Strings. String1 = C://files String2 = data.txt I want to concatenate the two strings while inserting a character between them, to get a new string: C://file/data.txt Here the character is an extra "/". Once this is done, I would like to convert the new string to a char[20]. Does anyone know how to perform this task? I would be very grateful.. Thanks KBL

      J Offline
      J Offline
      Jeff J
      wrote on last edited by
      #2

      There are no overloaded + or += operators for Strings in MC++, so one must use the plain member functions: String *String1 = S"C:\\files"; String *String2 = S"data.txt"; String1 = String::Concat(String1, S"\\", String2); //or much less efficiently... //String1 = String1->Insert(String1->Length, S"\\"); //String1 = String1->Insert(String1->Length, String2); char achCStr[20]; wchar_t *pGuts = PtrToStringArray(String1); int iChrs = ::WideCharToMultiByte(CP_ACP, 0, pGuts, String1->Length, achCStr, 20, NULL, NULL); achCStr[iChrs] = '\0'; PtrToStringArray() was posted earlier at http://www.codeproject.com/script/comments/forums.asp?forumid=3785&select=364715#xx364715xx[^] , and explained 2 posts earlier in that thread. String::Concat() returns a concatenated copy of between 2 and 4 strings, and since the lengths of all the Strings are calculated prior to assembling them into the returned String, should avoid multiple temporary copies usually associated with concatenation (nice!). Insert() works like strcat() when the start position is the same as the original String's length, but as you can see it requires an extra temporary String copy :(. It's undocumented, but when I do something like the above, I pass 'String1->Length+1' in WideCharToMultiByte(), which avoids having to add the terminating null character afterwards. Like CString and std::string, CLR Strings seem to always have a terminating null appended to them, and I have never seen a case where they haven't. Cheers

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups