简体   繁体   中英

String.Format inside of array

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.

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();

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
string[] a = {"{0}", "hit", "{1}"};
string[] b = {"Player", "Enemy"};
var result = string.Format(string.Join(" ", a), b);

should work quite well.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM