1

Essentially, I have a string that has placeholders that I am passing into String::Format(), as such:

"The {0} brown {1} jumped over the {2} {3}."

And I have another comma delimited string which contains the strings I wish to fill in to the formatting placeholders, as such:

"quick,fox,lazy,dog"

So it would only seem natural that to fill the format string I would Split(',') the second string, and pass that as the second argument in Format(first_str, second_split_array);

But splitting the second array produces a System::Array^ which Format() sees as only one object, thus getting angry and telling me my index must be less than or equal to the number of arguments passed.

Last I heard, String.Format() can use an array as the second arg, so... what do I have to do to get the string split in a way that Format() will see as all the args?

Edit:

The idea here is that the number of indexed elements is not the same every time. The goal I am trying to achieve is to take data sent from a server and fill it in to a template. The server sends back a comma delimited list and a template name. So, for the quick fox example it'd send what I have above, but other times It'll send, say, error, which uses:

"An error has occurred: {0} (Details: {1})"

and the server arguments "Error Name,Error text description and such."

2
  • But the amount of possible indexed results from the comma delimited list can be anything. I'll edit to elaborate. Commented Oct 20, 2012 at 18:10
  • I checked msdn again. msdn.microsoft.com/en-us/library/b1csw23d.aspx Commented Oct 20, 2012 at 18:11

2 Answers 2

2

Not sure what the question is really about, you can indeed pass an array of strings to String::Format(). The String::Split() method makes it simple to generate one:

int main(array<System::String ^> ^args)
{
    String^ text = "quick,fox,lazy,dog";
    array<String^>^ words = text->Split(',');
    String^ result = String::Format("The {0} brown {1} jumped over the {2} {3}.", words);
    Console::WriteLine(result);
    return 0;
}

Output:

The quick brown fox jumped over the lazy dog.

Sign up to request clarification or add additional context in comments.

Comments

0

It's probably because you're passing an array<String^>^ as the second argument to String::Format. If you want the overload of String::Format that takes an array as the second argument (and uses each element in the array as the formatting items), you need to pass an array<Object^>^ (where each Object^ is a String^).

3 Comments

I saw that in the MSDN article, I'm assuming there isn't a direct cast, so... I'd have to create an Array<Object^>^ and populate it with values from my string array?
I think so. It not a big change, as a String^ can be converted to an Object^ automatically (no cast). The only thing that will change is the declaration of the Array variable.
I was originally using System::Array^ MainArgs = server_response.Split(','); and changed it to array<System::Object^>^ MainArgs = server_response.Split(','); and I think it might be working fine... I'll do more tests.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.