C# 代码中 {0}\\{1} 的含义
正在处理一个我没有写过的文件,我只是想理解它。我知道下面的代码片段没什么可看的,但我只对一小部分感到困惑。
我只是想知道下面的“{0}\{1}”是什么意思,如果有人可以帮助我?
下面代码的当前结果给出了 c:\Output\Folder\Filename 的文件路径。
private string GetOutputPathForTarget()
{
return string.Format("{0}\\{1}", outputDirectory, settings.Name);
}
如果我想要,例如向该字符串添加另一个子目录,是否会像下面这样简单:
private string GetOutputPathForTarget()
{
return string.Format("{0}\\{1}", outputDirectory, settings.Name, "Images");
}
Am working with a file that I haven't written and I'm just trying to understand it. I understand that the below snippet is not much to go by but I'm only confused about one small part.
I'm just wondering for below, what the "{0}\{1}" means, if anyone can help me?
The current result of the code below gives a filepath of c:\Output\Folder\Filename.
private string GetOutputPathForTarget()
{
return string.Format("{0}\\{1}", outputDirectory, settings.Name);
}
If I wanted, for example add another subdirectory to this string, would it be as simple as:
private string GetOutputPathForTarget()
{
return string.Format("{0}\\{1}", outputDirectory, settings.Name, "Images");
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
不,这行不通。占位符的数量必须与 string.Format 的可选参数一样多。
另一方面,您应该使用 Path.Combine 来构建路径。 (请参阅此链接)
No, this doesn't work. There must be as many placeholders as optional arguments of
string.Format
.On the other hand, you should use
Path.Combine
to build paths. (See this link)“{0}\\{1}”
是 格式字符串 -{0}
表示“在此处插入第一个参数ToString
”,{1}
与第二个参数相同争论。如果您想插入另一个字符串,您需要告诉
String.Format
在哪里插入它:但是我强烈建议您使用
Path.Combine
"{0}\\{1}"
is a format string -{0}
means 'insert the first argumentToString
here',{1}
is the same for the second argument.If you wanted to insert another string, you would need to tell
String.Format
where to insert it:But then I would highly recommend you use
Path.Combine
instead这些是您希望插入常量字符串中指定位置的字符串的占位符。
看一下 String.Format 方法(字符串,对象[])
Those are place holders for the strings you wish to insert into the constant string at specified positions.
Have a look at String.Format Method (String, Object[])
MSDN 对此有很好的描述:
注意第二个样本没有意义,因为指定了三个参数,但只使用了两个。
通常,如果您想获取任意对象或值(例如浮点数)的格式化字符串表示形式,则可以使用 string.Format。
我们不知道
settings.Name
的类型,但如果它是string
类型,那么以下代码是等效的:或者:
后者具有您不知道的优点不必担心路径分隔符是否正确以及是否需要分隔符。
This is well described in MSDN:
Note that the second sample makes no sense as three parameters are specified but only two are used.
Typically you would use
string.Format
if you want to get a formatted string representation for arbitrary objects or values such as floating point numbers.We don't know the type of
settings.Name
but if it is of typestring
then the following code is equivalent:Or:
The latter has the advantage that you don't have to worry about the correct path separator and whether a separator is needed or not.
要添加子目录,您需要添加另一组
{}
。to add a subdirectory, you would need to add another set of
{}
.使用
String.Format
时,它会用附加参数替换 {0} 或 {1},从而创建一个组合字符串。例如 -
string.Format("{0}:{1}-{3}|{4}","a","b","c","d")
等于"a:bc|d"
对于您的最后一个要求,解决方案是:
with
String.Format
it replaces the {0} or {1} with the additional arguments, this creating a composed string.For example -
string.Format("{0}:{1}-{3}|{4}","a","b","c","d")
would equal"a:b-c|d"
For your last requirement, the solution would be:
你的例子不起作用!工作代码为:
{n} 被 string.Format 函数中的第 n 个参数替换。
但对于构建文件名,您应该更好地使用:
亲切的问候,
马库斯
Your example is NOT working! Working code would be:
{n} is Replaced by the n-th parameter in the string.Format Function.
But for Building FileNames you should better use:
Kind Regards,
Markus