C# 代码中 {0}\\{1} 的含义

发布于 2024-10-04 00:43:45 字数 532 浏览 0 评论 0原文

正在处理一个我没有写过的文件,我只是想理解它。我知道下面的代码片段没什么可看的,但我只对一小部分感到困惑。

我只是想知道下面的“{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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

同展鸳鸯锦 2024-10-11 00:43:45

不,这行不通。占位符的数量必须与 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)

开始看清了 2024-10-11 00:43:45

“{0}\\{1}”格式字符串 - {0} 表示“在此处插入第一个参数 ToString”,{1} 与第二个参数相同争论。

如果您想插入另一个字符串,您需要告诉 String.Format 在哪里插入它:

private string GetOutputPathForTarget()
{
    return string.Format("{0}\\{1}\\{2}", outputDirectory, settings.Name, "Images");
}

但是我强烈建议您使用 Path.Combine

"{0}\\{1}" is a format string - {0} means 'insert the first argument ToString 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:

private string GetOutputPathForTarget()
{
    return string.Format("{0}\\{1}\\{2}", outputDirectory, settings.Name, "Images");
}

But then I would highly recommend you use Path.Combine instead

无尽的现实 2024-10-11 00:43:45

这些是您希望插入常量字符串中指定位置的字符串的占位符。

看一下 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[])

悲凉≈ 2024-10-11 00:43:45

MSDN 对此有很好的描述:

string.Format方法< /p>

注意第二个样本没有意义,因为指定了三个参数,但只使用了两个。

通常,如果您想获取任意对象或值(例如浮点数)的格式化字符串表示形式,则可以使用 string.Format。

我们不知道 settings.Name 的类型,但如果它是 string 类型,那么以下代码是等效的:

return outputDirectory + "\\" + settings.Name;

或者:

return Path.Combine(outputDirectory, settings.Name);

后者具有您不知道的优点不必担心路径分隔符是否正确以及是否需要分隔符。

This is well described in MSDN:

string.Format method

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 type string then the following code is equivalent:

return outputDirectory + "\\" + settings.Name;

Or:

return Path.Combine(outputDirectory, settings.Name);

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.

君勿笑 2024-10-11 00:43:45

要添加子目录,您需要添加另一组 {}

private string GetOutputPathForTarget()
{
    return string.Format("{0}\\{1}\\{2}", outputDirectory, settings.Name, "Images");
}

to add a subdirectory, you would need to add another set of {}.

private string GetOutputPathForTarget()
{
    return string.Format("{0}\\{1}\\{2}", outputDirectory, settings.Name, "Images");
}
上课铃就是安魂曲 2024-10-11 00:43:45

使用 String.Format 时,它会用附加参数替换 {0} 或 {1},从而创建一个组合字符串。

例如 - string.Format("{0}:{1}-{3}|{4}","a","b","c","d") 等于"a:bc|d"

对于您的最后一个要求,解决方案是:

return string.Format("{0}\\{1}\\Images", outputDirectory, settings.Name);

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:

return string.Format("{0}\\{1}\\Images", outputDirectory, settings.Name);
一个人的夜不怕黑 2024-10-11 00:43:45

你的例子不起作用!工作代码为:

return string.Format("{0}\\{2}\\{1}", outputDirectory, settings.Name, "Images"); 

{n} 被 string.Format 函数中的第 n 个参数替换。

但对于构建文件名,您应该更好地使用:

return System.IO.Path.Combine(part1, part2, part3);

亲切的问候,
马库斯

Your example is NOT working! Working code would be:

return string.Format("{0}\\{2}\\{1}", outputDirectory, settings.Name, "Images"); 

{n} is Replaced by the n-th parameter in the string.Format Function.

But for Building FileNames you should better use:

return System.IO.Path.Combine(part1, part2, part3);

Kind Regards,
Markus

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文