条件字符串格式
本文关键字:格式 字符串 条件 | 更新日期: 2023-09-27 18:21:36
我正在尝试使用条件字符串格式来显示找到的记录或不是这样"找到10行中的5行"
string msg = "{0:#;;} of {1:#;;} {2:rows;no rows;row} found";
return string.Format(msg, searchItems, totalItems, totalItems - 1);
一切都很好,直到totalItems为0,因为那时我得到了这样的消息集。"找不到第行,共行"(错误)
我想要这样的"未找到行"
searchItems = 0 ; totalItems = 0 ==> "no rows found"
searchItems = 1 ; totalItems = 1 ==> "1 row found"
searchItems = 2 ; totalItems = 5 ==> "2 of 5 rows found"
您可以将.ToString()
添加到searchItems
变量中,例如:
string msg = "{0:#;;} of {1:#;;} {2:rows;no rows;row} found";
return string.Format(msg, searchItems.ToString(), totalItems, totalItems - 1);
假设searchItems
和totalItems
都是0
:
未找到0行
假设searchItems
和totalItems
都是1
:
1行中的1行找到
假设searchItems
是2
并且totalItems
是5
:
5行中有2行找到
然而,我会重写它并使用if语句,它可能有更多的代码行,但可读性更强。