格式化字符串的代码
string.Format("{0} -> {1} {2} .{3}, {4}", A, alpha, X, beta, a);
我有这个值
A = "S'"
alpha = "S"
X = (null)
beta = (null)
a = "$"
输出:
"S' -> S., $" //ok good output
现在有了这个值:
A = "S'"
alpha = (null)
X = "S"
beta =(null)
a = "$"
输出:
"S' -> S ., $" //I don't like this :(
如何为这两种情况获得相同的格式?
"S' -> S., $" //Ideal case
答案 0 :(得分:2)
我认为你正在寻找这个,但它并不完全清楚。基本上,如果alpha不为null,它将打印alpha,否则将打印X:
string.Format("{0} -> {1}.{2}, {3}", A, (alpha ?? X ), beta, a);
答案 1 :(得分:-1)
使用字符串格式时,第一个参数将替换为{0},第二个参数将替换为{1},依此类推。当您将alpha设置为null并将X设置为S时,编译器将{1}替换为null,将{2}替换为S