1

我这里有一个字符串,

return  string.Format("/abcXYZ990099/abc.aspx?IDA={0}&Name={1}&Teacher={2}",
                ID, Name, Teacher);

现在由于需求改变,我也需要从数据库中获取“abcXYZ990099”,是否可以做这样的事情,

return  string.Format("/{3}/abc.aspx?IDA={0}&Name={1}&Teacher={2}",
                    ID, Name, Teacher, NewPropertyValue);
4

3 回答 3

7

是的,你可以这么做。但我会重新索引占位符并重新排序参数以适应它们应该出现在返回字符串中的顺序,例如

return  string.Format("/{0}/abc.aspx?IDA={1}&Name={2}&Teacher={3}",
                    NewPropertyValue, ID, Name, Teacher);
于 2013-07-04T11:05:01.917 回答
1

This is perfectly OK, format items can appear in any order in the string.

For example, when format strings are stored as localized resources, the format items might be in a culture-specific order. For example, to display a full name, you could use:

String.Format(Resources.FullNameFormatString, firstName, middleName, lastName)

And the display order might depend on the culture, e.g.:

en-US: "{0} {1} {2}"  // First Middle Last

fr-FR: "{2}, {0} {1}" // Last, First Middle

You might even have a localized version that doesn't use one of the format items (e.g. middle name):

"{2), {0}" // Last, First (middle name not used)

Of course, in your example it probably makes more sense to reorder the items, as others have said.

于 2013-07-04T11:10:29.967 回答
0

你可以做。但是改变顺序,因为代码必须是可以理解的。

string.Format("/{0}/abc.aspx?IDA={1}&Name={2}&Teacher={3}",NewPropertyValue, ID, Name, Teacher);
于 2013-07-04T11:09:28.650 回答