What does {0} mean in String.Format?

余生颓废 提交于 2019-11-29 18:25:49

{0} is a placeholder for the first object given; in this case that's filename, so it will insert whatever filename evaluates to in place of {0}. Similarly, of course you could use {1} and that would be replaced with the second parameter passed, etc.

{0} refers to the second parameter passed into String.Format. {1} refers to the third, {2} to the fourth, etc. For example:

String.Format("The {0} brown {1} jumps {2} the {3} dog.", "quick", "fox", "over", "lazy")

Evaluates to

"The quick brown fox jumps over the lazy dog."

It means the value of filename. Please see MSDN.

It is an indexer to the arguments presented after the “Select * from {0}” and can be combined with format specifiers as well. See the documentation for String.Format Method. However, you should NEVER EVER create a SQL command this way as it is vulnerable to SQL Injection attacks. You should always parameterize SQL queries. See the How To: Protect From SQL Injection in ASP.NET article on MSDN.

The {0} is a reference to the first argument passed after the format string.

In your case, "Select * from {0}" is the format string and filename is the first argument.

As an example:

String.Format("Select * from {0}{1}{0}","this","database")

would return:

"Select * from thisdatabasethis"

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!