开发者

How do I make a method that returns a string array in C++?

开发者 https://www.devze.com 2023-03-23 16:02 出处:网络
I have a problem, I am trying to convert from C# to C++/CLI and I don\'t know how to return a string array in a method.

I have a problem, I am trying to convert from C# to C++/CLI and I don't know how to return a string array in a method. The problem is that strings are showing up as numbers instead of strings.

Here is the Method:

static array<String^> ^Split(String^ Victim, char SplitPoint)
{
int Index=0;
        for each(char Char in Victim)
            if(Char==SplitPoint)
                Index++;
        array<String^> ^SplitStrings = gcnew array<String^>;
        Index=0;
        for each(char Char in Victim)
        {
            if(Char==SplitPoint)
                Index++;
            else
                SplitStrings[Index]=SplitStrings[Index]+Char;
        }
        return SplitStrings;
    };

and the original method in C# looks like this:

    public static string[] Split(string Victim, char SplitPoint)
    {
        int Index = 0;
        foreach (char Char in Victim)
            if (Char == SplitPoint)
                Index++;
        string[] SplitStrings = new string[Index +开发者_如何学运维 1];
        Index = 0;
        foreach (char Char in Victim)
        {
            if (Char == SplitPoint)
            {
                Index++;
            }
            else
                SplitStrings[Index] = SplitStrings[Index] + Char;
        }
        return SplitStrings;
    }


It's really unclear what your actual question is, so here's a direct translation of your C# code to C++/CLI:

public:
    static array<String^>^ Split(String^ Victim, wchar_t SplitPoint)
    {
        int Index = 0;
        for each (wchar_t Char in Victim)
            if (Char == SplitPoint)
                Index++;
        array<String^>^ SplitStrings = gcnew array<String^>(Index + 1);
        Index = 0;
        for each (wchar_t Char in Victim)
        {
            if (Char == SplitPoint)
                Index++;
            else
                SplitStrings[Index] = SplitStrings[Index] + Char;
        }
        return SplitStrings;
    }

In particular, note that char in C++/CLI equates to System::SByte, not System::Char -- the native name for the latter is wchar_t. (Also, you didn't specify the dimensions of SplitStrings.)

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号