开发者

String.Format - Problem with Argument list

string nr = "42245555" //just an example number
string expression = "{5}{6}/{7}{8}";

string res = String.Format(expression, new string[] {
nr[0].ToString(),nr[1].ToString(),
nr[2].ToString(),开发者_如何学JAVA nr[3].ToString(), 
nr[4].ToString(), nr[5].ToString(), 
nr[6].ToString(), nr[7].ToString() 
});

Why is this not working and how can I solve it? I want expression to be either "{5}{6}/{7}{8}" or "{0}{3}/{7}{1}" or whatever the user wants.


You must supply at least the same number of parameters (or an array with at least the same number of elements) as the highest placeholder value plus one (placeholder values are zero indexed)

Max placeholder value {3}, you must supply at least four additional parameters.

Try this:

string res = String.Format(expression,
  nr[0], nr[1],
  nr[2], nr[3], 
  nr[4], nr[5], 
  nr[6], nr[7]);

Note that I took out new string[] { ... } I also took out all the ToString() because they are not required.


The relevant overload of Format is:

public static string Format(string format,  params Object[] args)

This means you can either call it like this:

Format("...",new object[]{...})//Each array element is used

Or with the objects directly as parameters:

Format("...",object1, object2,...)//the objects are used

Unlike what I originally thought, a string[] does indeed go into the first variant of this code. This is due to array covariance in C#/.net which allows a string[] to be implicitly converted to a object[].

string.Format("{0} {1}",new string[]{"A","B"})

prints A B.

On the other hand if you try similar code with a int[] it won't work:

string.Format("{0} {1}",new int[]{1,2})

Will throw an exception because it goes into the second variation, and thus only a single parameter of type int[] is seen by string.Format. This difference is because array covariance only works on arrays with members that are a reference type like string and not a value type like int. This means int[] is not implicitly convertible to object[].

So the problem with your original code is probably just that you used the index {8} which is out of range.


The parameters in expression must start from "{0}" and the array must contain the same number of parameters as the expression and it must be an array of objects : new object[]{...


  • Your parameters are numbered 0..7, easy to read back. Yet you use {8} : Index out of Range
  • You don't need the new string[] { } around the parameters. It is allowed though.
  • without the new string[] { } you don't need the .ToString() calls either.


IF ur user wants {5}{6}/{7}{8} , then ur code will be:

String.Format("{0}{1}/{2}{3}",nr[4],nr[5],nr[6],nr[7]);

ur indexes in the expression must always start with 0..In my example if u want to display this for strings.. u start by 0,1,2,3.. 0 for nr[4], 1 for nr[5], 2 for nr[6], 3 for nr[7], etc..


You have to use:

nr.Substring(...)

in String.Format([expression], [values]) expression is a string represent a string with placeholders while values are inserted in those placeholders.

updated

string nr = "42245555" //just an example number   


string res = String.Format("{0}{1}/{2}{3}", new string[] {
nr[5].ToString(),nr[6].ToString(),
nr[7].ToString(), nr[8].ToString()
});

or

string res = String.Format("{0}{1}/{2}{3}", new string[] {
nr[0].ToString(),nr[3].ToString(),
nr[7].ToString(), nr[1].ToString()
});
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜