你能在格式字符串中包含条件检查代码吗?

时间:2023-01-14 10:50:50

I have a custom control which is part of a list. The text that it should display is set by assigning a value to its Text property. At runtime, this text is loaded from a database. I want to prepend this text with the string "Toelichting: " (Dutch for "Explanation"). I can accomplish this by setting the FormatString property of the control to the following:

我有一个自定义控件,它是列表的一部分。应显示的文本是通过为其Text属性赋值来设置的。在运行时,此文本从数据库加载。我想在文本前添加字符串“Toelichting:”(荷兰语为“说明”)。我可以通过将控件的FormatString属性设置为以下来完成此操作:

"Toelichting: {0}"

Now, if the text that is loaded is an empty string, I want to display "Toelichting: –", so with an en-dash at the end. Otherwise, I want to display "Toelichting: MyText". Is it possible to add some condition-checking code to the FormatString, such that I can check if the parameter is not empty?

现在,如果加载的文本是一个空字符串,我想显示“Toelichting: - ”,所以最后加上一个短划线。否则,我想显示“Toelichting:MyText”。是否可以向FormatString添加一些条件检查代码,以便我可以检查参数是否为空?

4 个解决方案

#1


No, there's no way to do that.

不,没有办法做到这一点。

#2


Why not just this?

为什么不呢?

string.Format("Toelichting: {0}", string.IsNullOrEmpty(explanation) ? "–" : explanation);

I don't think there's a way to embed this within the format string.

我认为没有办法将其嵌入格式字符串中。

#3


You can do it like this:

你可以这样做:

String.Format("Toelichting: {0}", 
    (String.IsNullOrEmpty(yourstr)) ? "-" : yourstr);

Not perfect but its relatively compact and readable.

不完美,但相对紧凑和可读。

#4


If you're doing this sort of thing a lot then consider writing your own formatter so that you could write code like this...

如果你做了很多这样的事情,那么考虑编写自己的格式化程序,以便你可以编写这样的代码......

foo = string.Format(new MyFormatter(), "Toelichting: {0:explanation}", bar);

foo = string.Format(new MyFormatter(),“Toelichting:{0:explanation}”,bar);

MyFormatter would implement IFormatProvider and ICustomFormatter.

MyFormatter将实现IFormatProvider和ICustomFormatter。

Check out this...

看看这......

.NET: Is there a String.Format form for inserting the value of an object property into a string?

.NET:是否有String.Format表单用于将对象属性的值插入字符串?

... which is probably more complicated than you need (as it deals with reflection and works with any object)

...这可能比你需要的更复杂(因为它处理反射并适用于任何对象)

#1


No, there's no way to do that.

不,没有办法做到这一点。

#2


Why not just this?

为什么不呢?

string.Format("Toelichting: {0}", string.IsNullOrEmpty(explanation) ? "–" : explanation);

I don't think there's a way to embed this within the format string.

我认为没有办法将其嵌入格式字符串中。

#3


You can do it like this:

你可以这样做:

String.Format("Toelichting: {0}", 
    (String.IsNullOrEmpty(yourstr)) ? "-" : yourstr);

Not perfect but its relatively compact and readable.

不完美,但相对紧凑和可读。

#4


If you're doing this sort of thing a lot then consider writing your own formatter so that you could write code like this...

如果你做了很多这样的事情,那么考虑编写自己的格式化程序,以便你可以编写这样的代码......

foo = string.Format(new MyFormatter(), "Toelichting: {0:explanation}", bar);

foo = string.Format(new MyFormatter(),“Toelichting:{0:explanation}”,bar);

MyFormatter would implement IFormatProvider and ICustomFormatter.

MyFormatter将实现IFormatProvider和ICustomFormatter。

Check out this...

看看这......

.NET: Is there a String.Format form for inserting the value of an object property into a string?

.NET:是否有String.Format表单用于将对象属性的值插入字符串?

... which is probably more complicated than you need (as it deals with reflection and works with any object)

...这可能比你需要的更复杂(因为它处理反射并适用于任何对象)