有什么方法可以在编译时检查格式字符串吗?
例子:
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) 我正在编写一个代码生成工具,经常会有类似的行
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会正确地处理它.
我该怎么做呢?
我有以下字符串
"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#中格式化十进制类型时带走小数部分?
decimal a = 1.00m;
String.Format("{0}", a); // result is 1.00 Should be 1, HOW?
Run Code Online (Sandbox Code Playgroud) 我正在使用此命令:
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宽,表示以下参数相差三。
有什么解决方法吗?
检查以下代码段,
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输出
我发现了一段这样的代码
int a = 100;
string str = $"{a:0.00}";
Console.WriteLine(str);
Run Code Online (Sandbox Code Playgroud)
结果是"100.00"
$具有与string.Format相同的功能,我想知道哪个版本的C#.
什么是使用*的.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) 查看 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 本身的类型转换?
这可能会在这里被问过几次......我想在字符串的每四个字符之间添加空格(8888319024981442)。我的字符串长度正好是 16。 String.format没有帮助
避免使用 split 或在内存中创建多个字符串。
有没有可以快速使用的 kotlin 函数/String.format。
string.format ×10
c# ×6
string ×4
python ×2
.net ×1
args ×1
cultureinfo ×1
datetime ×1
dollar-sign ×1
escaping ×1
java ×1
kotlin ×1
python-3.x ×1
replaceall ×1
tkinter ×1