标签: string.format

格式化字符串-编译时检查

有什么方法可以在编译时检查格式字符串吗?

例子:

Console.WriteLine("{0} is a really {1} site", "stackoverflow.com", "cool");//this will run
Run Code Online (Sandbox Code Playgroud)

//这将给出一个异常,因为只提供了一个参数

Console.WriteLine("{0} is a really {1} site", "stackoverflow.com");

Exception:"Index (zero based) must be greater than or equal to zero and less than the size of the argument list."
Run Code Online (Sandbox Code Playgroud)

如果格式字符串的格式不正确(即此处 1 之后缺少“}”)

Console.WriteLine("{0} is a really {1 site", "stackoverflow.com","cool");

Exception: Input string was not in a correct format.
Run Code Online (Sandbox Code Playgroud)

.net c# string string.format

2
推荐指数
1
解决办法
1222
查看次数

如何让String.Format不解析{0}

我正在编写一个代码生成工具,经常会有类似的行

StringBuilder sp = new Stringbuilder();
sp.AppendFormat("        public {0}TextColumn()\n", className);
sp.AppendLine("        {"
sp.AppendLine("            Column = new DataGridViewTextBoxColumn();");
sp.AppendFormat("            Column.DataPropertyName = \"{0}\";\n", columnName);
Run Code Online (Sandbox Code Playgroud)

然而,我遇到的问题是当我遇到像这样的一行时.

sp.AppendFormat("return String.Format(\"{0} = '{0}'\", cmbList.SelectedValue);", columnName);
Run Code Online (Sandbox Code Playgroud)

我想第一个{0}在转向任何COLUMNNAME的价值,但我想seccond {0}独处因此内部String.Format会正确地处理它.

我该怎么做呢?

c# string.format code-generation

2
推荐指数
2
解决办法
294
查看次数

转义String.Format占位符

我有以下字符串

"ListId={1CC88B01-E60F-45D1-8B3C-28852574156D}&ID={0}&ContentTypeID=0x01003D458D19EF31D845B3A7727B0F2F8FC8"
Run Code Online (Sandbox Code Playgroud)

我想这样使用它

String.Format("ListId={1CC88B01-E60F-45D1-8B3C-28852574156D}&ID={0}&ContentTypeID=0x01003D458D19EF31D845B3A7727B0F2F8FC8", MyValue)

WHERE MyValue would replace ID={0}
Run Code Online (Sandbox Code Playgroud)

然而这引发了一个System.FormatException.显然这是因为{1CC88B01-E60F-45D1-8B3C-28852574156D} guid它使用了string.format占位符值.现在我可以string.format()根据需要简单地拆分字符串调用并将其重新连接在一起,但有可能通过某种方式我可以通过某种方式"转义"占位符值来避免这样做吗?

c# string.format escaping

2
推荐指数
1
解决办法
843
查看次数

如何使用String.Format(c#)删除小数部分

如何在C#中格式化十进制类型时带走小数部分?

        decimal a = 1.00m;

        String.Format("{0}", a); // result is 1.00 Should be 1, HOW?
Run Code Online (Sandbox Code Playgroud)

c# string.format

2
推荐指数
1
解决办法
2940
查看次数

TkInter ListBox和.format的使用

我正在使用此命令:

self.licenseBox.insert(END, "{:30}{:90}{:20}{:5}".format(item[0],
                                                    item[1], item[2], item[3]))
Run Code Online (Sandbox Code Playgroud)

但是.format将添加项目,然后添加列宽。例如if item[0] = "foo",第一列为33宽,表示以下参数相差三。

有什么解决方法吗?

python string string.format tkinter

2
推荐指数
1
解决办法
2506
查看次数

String.format()DateTime具有阿拉伯文化

检查以下代码段,

namespace TestDateConvertion
{
    class Program
    {
        static void Main(string[] args)
        {
            DateTime testValue = new DateTime(2013, 12, 15, 15, 33, 44);

            CultureInfo culture = new CultureInfo("ar-SA");
            string stringValue = string.Format(culture, "{0:d} {0:HH:mm:ss}", testValue);
            Console.WriteLine(stringValue);
            Console.ReadLine();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

它给出了输出

22/02/35 15:33:44

我不知道这是怎么可能的.什么是那里的35输出

c# string.format datetime cultureinfo

2
推荐指数
1
解决办法
4333
查看次数

哪个版本的C#以字符串格式使用$

我发现了一段这样的代码

int a = 100;
string str = $"{a:0.00}";
Console.WriteLine(str);
Run Code Online (Sandbox Code Playgroud)

结果是"100.00"

$具有与string.Format相同的功能,我想知道哪个版本的C#.

c# string.format dollar-sign

2
推荐指数
1
解决办法
8420
查看次数

星号`*`如何在Python 3中的字符串格式化方法`.format(*)`中工作?

什么是使用*.format(*)?在下面的格式函数中使用它时,print(new_string.format(*sum_string))它会将输出中sum_string的值从18更改为1为什么会发生这种情况?我已阅读以下链接*args,**kwargs但无法理解这是如何适用于该.format()功能

**(双星/星号)和*(星号/星号)对参数有什么作用?

sum_string = "18"
new_string = "This is a new string of value {}"

print(new_string.format(sum_string)) #it provides an output of value 18
print(new_string.format(*sum_string)) #it provides an output of value 1
Run Code Online (Sandbox Code Playgroud)

python string.format string-formatting args python-3.x

2
推荐指数
1
解决办法
1029
查看次数

为什么 String.format 接受整数代替 %s?

查看 Java String.format() 的一些参考资料,我看到他们提到它具有以下格式:

public static String format(String format, Object... args)
Run Code Online (Sandbox Code Playgroud)

所以它允许我写以下内容:

String newStr = String.format("%s,%s,%s", 1, 2, 3);
Run Code Online (Sandbox Code Playgroud)

但我不能这样做:

String newStr = String.format("%d,%d,%d", "1", "2", "3");
Run Code Online (Sandbox Code Playgroud)

为什么我可以使用整数代替 %s 而不是反之亦然?它是否在进行从 int 到 string 本身的类型转换?

java string string.format string-formatting

2
推荐指数
1
解决办法
48
查看次数

使用 kotlin 字符串函数/字符串格式添加空格

这可能会在这里被问过几次......我想在字符串的每四个字符之间添加空格(8888319024981442)。我的字符串长度正好是 16。 String.format没有帮助

避免使用 split 或在内存中创建多个字符串。

有没有可以快速使用的 kotlin 函数/String.format。

string string.format replaceall kotlin

2
推荐指数
1
解决办法
1812
查看次数