2

So I have the string format split into two arrays:

String:

{ "{0}", "hit", "{1}" } 

Values:

{ "Player", "Enemy" }

is there a way to format this ? Can I still use String.Format?

EDIT:

I believe that I have not described the issue well:

The expected result would be new array or the first array edited like:

{ "Player" , "hit" , "Enemy" } 

Please keep in mind that value "Enemy" can also be "Enemy 1" or "Enemy Strong" etc.

4
  • 1
    No. What is the first array supposed to represent? Why isn't it "{0} hit {1}"? You can join the first array using a space to create such a format string, if that's what you mean... Commented Jun 29, 2015 at 9:39
  • 1
    Is the string really: string text = "{ \"{0}\", \"hit\", \"{1}\" }"? Commented Jun 29, 2015 at 9:39
  • What is your expected outcome? Commented Jun 29, 2015 at 9:43
  • It is just an example, I am pulling string format and values from API. Then I am splitting it to array as I need all values separate in different variables. Commented Jun 29, 2015 at 9:43

3 Answers 3

4

Because of the input, I expect you want to create a string[] as result rather than a string. To do that, you can use Linq:

var s = new[] {"{0}", "hit", "{1}"};
var values = new [] {"Player", "Enemy"};

var result = s.Select((a)=>string.Format(a, values)).ToArray();
Sign up to request clarification or add additional context in comments.

Comments

1

You can do it something like:

var s = new[] {"{0}", "hit", "{1}"};
var values = new [] {"Player", "Enemy"};

var result = String.Format(String.Join(" ", s), values);

Output:

Player hit Enemy

1 Comment

Not the output OP expected.
0
string[] a = {"{0}", "hit", "{1}"};
string[] b = {"Player", "Enemy"};
var result = string.Format(string.Join(" ", a), b);

should work quite well.

Comments

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.