格式化我的字符串

发布于 2024-08-06 09:44:24 字数 216 浏览 4 评论 0原文

如何格式化这样的字符串:

string X = "'{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}','{8}','{9}','{10}','{11}','{12}','{13}'",????

我记得我曾经能够在末尾放置一个逗号并指定要分配给 {0}、{1} 等的实际数据。

有任何帮助吗?

How can I format a string like this:

string X = "'{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}','{8}','{9}','{10}','{11}','{12}','{13}'",????

I remember I used to be able to put a comma at the end and specify the actual data to assign to {0},{1}, etc.

Any help?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(8

勿忘初心 2024-08-13 09:44:25

您是否在寻找:

String.Format("String with data {0}, {1} I wish to format", "Foo", "Bar");

将导致

“带有数据 Foo 的字符串,我希望格式化的 Bar”

are you looking for:

String.Format("String with data {0}, {1} I wish to format", "Foo", "Bar");

would result in

"String with data Foo, Bar I wish to format"

木有鱼丸 2024-08-13 09:44:25

使用 string.Format

var output = string.Format("'{0}', '{1}'", x, y);

Use string.Format as in

var output = string.Format("'{0}', '{1}'", x, y);
逆流 2024-08-13 09:44:24

使用 string.Format 方法,例如:


string X = string.Format("'{0}','{1}','{2}'", foo, bar, baz);

Use string.Format method such as in:


string X = string.Format("'{0}','{1}','{2}'", foo, bar, baz);
策马西风 2024-08-13 09:44:24

如果您有字符串数组中的值,另一种方法是使用 Join:(

string x = "'" + String.Join("','", valueArray) + "'";

只是想与 89724362 用户不同,他们将向您展示如何使用 String.Format...;)

An alternative is to use Join, if you have the values in a string array:

string x = "'" + String.Join("','", valueArray) + "'";

(Just wanted to be different from the 89724362 users who will show you how to use String.Format... ;)

Smile简单爱 2024-08-13 09:44:24
String.Format("'{0}', '{1}'", arg0, arg1);
String.Format("'{0}', '{1}'", arg0, arg1);
桃扇骨 2024-08-13 09:44:24

String.Format 方法接受一个格式字符串,后跟一对要格式化的变量。格式字符串由占位符组成,这些占位符本质上是放置传递给函数的变量值的位置。

Console.WriteLine(String.Format("{0}, {1}, {2}", var1, var2, var3));

The String.Format method accepts a format string followed by one to many variables that are to be formatted. The format string consists of placeholders, which are essentially locations to place the value of the variables you pass into the function.

Console.WriteLine(String.Format("{0}, {1}, {2}", var1, var2, var3));
情绪少女 2024-08-13 09:44:24

你的问题有点含糊,但你的意思是:

// declare and set variables val1 and val2 up here somewhere
string X = string.Format("'{0}','{1}'", val1, val2);

还是你在要求别的东西?

Your question is a bit vague, but do you mean:

// declare and set variables val1 and val2 up here somewhere
string X = string.Format("'{0}','{1}'", val1, val2);

Or are you asking for something else?

夏了南城 2024-08-13 09:44:24

使用 string.format,并将单独的格式说明符放在大括号内,在数字和冒号之后,如

   string s = string.Format(
        " {0:d MMM yyyy} --- {1:000} --- {2:#,##0.0} -- {3:f}",
        DateTime.Now, 1, 12345.678, 3e-6);

and 所示,正如您从示例中看到的,您不需要单引号来描述文字,任何不在大括号内的内容都会按字面意思输出

use string.format, and put individual format specifiers inside the braces, after the number and a colon, as in

   string s = string.Format(
        " {0:d MMM yyyy} --- {1:000} --- {2:#,##0.0} -- {3:f}",
        DateTime.Now, 1, 12345.678, 3e-6);

and, as you can see from the example, you don;t need the single quotes to delineate literals, anything not inside braces will be output literally

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