locked
How to copy contents of an array into a string RRS feed

  • Question

  • Hello,  its been 4 days that I am stuck like a donkey in mud.
    (BTW, i did review similar questions but no help)

    I have an array that has been initialized like this


    /////////////////////C O D E ///////////////////////////
    array<Char>^   arr;
    String^             Str;

    arr = gcnew array<Char> (5);

    arr[0] = 'a';
    arr[1] = 'b';
    arr[2] = 'c';
    arr[3] = 'd';
    arr[4] = 'e';
    ////////////////////////////////////////////////////////

    so far so good, I created an array of size 5 and inserted characters 'a','b','c','d' and 'e' into its elements.

    Now here is my simple question:  HOW CAN I TRANSFER THE CONTENT OF THE ARRAY(arr[]) into the string Str ? So Str should contain this "abcde"  ? Is there a simple method?


    Thank you
    Wednesday, December 23, 2009 1:26 AM

Answers

  • Hello Krvcun,

    String has constructor to take Char array. See the code snippet.

     array<Char>^  arr;
     String^       Str;
    
     arr = gcnew array<Char> (5);
    
     arr[0] = 'a';
     arr[1] = 'b';
     arr[2] = 'c';
     arr[3] = 'd';
     arr[4] = 'e';
    
     // String has constructor to take Char array.
     Str = gcnew String( arr );
    

    Best Regards,
    Jijo.

    http://weseetips.com[^] Visual C++ tips and tricks. Updated daily.
    • Marked as answer by Wesley Yao Tuesday, December 29, 2009 2:44 AM
    Wednesday, December 23, 2009 8:09 AM

All replies

  • Hi, I think this what u want

    #include<iostream>
    #include<string>
    using namespace std;
    void main()

    {

    char arr[6]={'a','b','c','d','e'};

    string str;
    str=arr;

    cout<<str<<endl;

    //u can also do this

    cout<<arr<<endl;

    }

    I hope it's helpfull :)

    Wednesday, December 23, 2009 3:54 AM
  • Hello Krvcun,

    String has constructor to take Char array. See the code snippet.

     array<Char>^  arr;
     String^       Str;
    
     arr = gcnew array<Char> (5);
    
     arr[0] = 'a';
     arr[1] = 'b';
     arr[2] = 'c';
     arr[3] = 'd';
     arr[4] = 'e';
    
     // String has constructor to take Char array.
     Str = gcnew String( arr );
    

    Best Regards,
    Jijo.

    http://weseetips.com[^] Visual C++ tips and tricks. Updated daily.
    • Marked as answer by Wesley Yao Tuesday, December 29, 2009 2:44 AM
    Wednesday, December 23, 2009 8:09 AM
  • No syrian star, it didn't help, please note that my project is a 'windows form' project.  It is so unforgiving.  The slightest things I do, I get compiler error(where used to be OK with the regular 'C' I used to work with in the past) .  For example, I have tried  the one line of code you have suggested --->  str=arr;  but it generates compiler error on type conversion,  I have also tried the following two lines of code with no compiler errors, but still str it is NOT set to "abcde" :

    str =

    dynamic_cast<String^>(arr);

    str = arr->ToString();

    Wednesday, December 23, 2009 8:18 AM
  • Thanks Jijo Raj, it helped.   the donkey is finally free.
    Wednesday, December 23, 2009 9:00 AM
  • No syrian star, it didn't help, please note that my project is a 'windows form' project.  It is so unforgiving.  The slightest things I do, I get compiler error(where used to be OK with the regular 'C' I used to work with in the past) .
    You have to understand that when you use WinForms in Visual C++, you are not using C++, but rather a new language called C++/CLI. C++/CLI is essentially a combination of standard C++ and C# in a single (rather complex) language.

    You should also know that Microsoft is no longer promoting C++/CLI as a means of developing .NET GUI programs. Rather you should use C# for managed .NET applications (WinForms or WPF) or standard C++ with MFC for native (unmanaged) applications.

    The intended use for C++/CLI is "wrapping" native C++ code into managed class libraries that can be used from C# or VB.NET.

    David Wilkinson | Visual C++ MVP
    Wednesday, December 23, 2009 10:39 AM
  • OH MY GOD, YOU JUST SHED BIG LIGHT ON MY DARKNESS,  THANK YOU davewilk

    I knew something was fishy here?

    I am little clueless on these things.   PLEASE HELP
    OK, so I have to use C# for managed .NET applications, 

    Here comes my BIG question:

    ---> To use C# for managed .NET applications, can I still use VC++ 2008 Express Edition by just creating a new PROJECT with different properties to make it C# for managed .NET applications and then transfer/convert my existing code into that ?

    If the anwser to the BIG question is 'NO ', then what should I use ?


    Thank you
    Thursday, December 24, 2009 1:30 AM
  • You should use Visual C# Express.

    David Wilkinson | Visual C++ MVP
    Thursday, December 24, 2009 5:05 AM
  • Thank you Davewilk, if it was up to me you would get your sixth medal !

    I hope Visual C# Express is also free like VC++
    Thursday, December 24, 2009 5:54 AM
  • I hope Visual C# Express is also free like VC++
    Indeed. All express editions are free.

    Best Regards,
    Jijo.


    http://weseetips.com[^] Visual C++ tips and tricks. Updated daily.
    Thursday, December 24, 2009 7:34 AM