寻找一种快速测试C#格式字符串的工具

时间:2023-01-14 16:24:02

I am constantly forgetting what the special little codes are for formatting .NET strings. Either through ToString() or using String.Format(). Alignment, padding, month vs. minute (month is uppercase M?), abbreviation vs. full word, etc. I can never remember.

我经常忘记用于格式化.NET字符串的特殊小代码。通过ToString()或使用String.Format()。对齐,填充,月份与分钟(月份是大写M?),缩写与完整单词等等。我永远不会记得。

I have the same problem with regexes, but luckily there's Expresso to help me out. It's awesome.

我对正则表达式有同样的问题,但幸运的是有Expresso来帮助我。这很棒。

Is there a tool like Expresso for experimenting with formatted strings on standard types like DateTime and float and so on?

是否有像Expresso这样的工具用于在DateTime和float等标准类型上试验格式化字符串?

7 个解决方案

#1


PowerShell works great for testing format strings. From PowerShell you can load your assembly and work with the objects and methods you want to test. You could also just create a string on the command line and test out different formatting options.

PowerShell非常适合测试格式字符串。在PowerShell中,您可以加载程序集并使用要测试的对象和方法。您还可以在命令行上创建一个字符串,并测试不同的格式选项。

You can use the static method from the string class:

您可以使用字符串类中的静态方法:

$teststring = 'Currency - {0:c}.  And a date - {1:ddd d MMM}.  And a plain string - {2}'
[string]::Format($teststring, 160.45, Get-Date, 'Test String')

Or PowerShell has a built in format operator

或者PowerShell具有内置格式运算符

$teststring = 'Currency - {0:c}.  And a date - {1:ddd d MMM}.  And a plain string - {2}'
$teststring -f 160.45, Get-Date, 'Test String'

#2


I just found this:

我刚发现这个:

http://rextester.com/

Simply paste in your format string, and run the code.

只需粘贴您的格式字符串,然后运行代码即可。

It would also be simple enough to create a windows or console project that does exactly that.

创建一个完全相同的窗口或控制台项目也很简单。

#3


Snippet Compiler is a great tool in general for quick small app testing. Instead of cluttering your Visual Studio with a million ConsoleApplication79 projects, just use this. I have it and use it constantly.

Snippet Compiler是一款非常棒的工具,适用于快速小应用测试。不要使用一百万个ConsoleApplication79项目混乱Visual Studio,只需使用它。我有它并且经常使用它。

#5


LinqPad is a great tool that handles this sort of thing brilliantly, even though it's tangential to its primary function (of troubleshooting Linq syntax).

LinqPad是一个出色的工具,可以很好地处理这类事情,即使它与其主要功能(对Linq语法进行故障排除)相关。

Just enter the expression with the language selector set to "C# Expression" (or "VB.net Expression") and the database set to "None." For example:

只需输入语言选择器设置为“C#Expression”(或“VB.net Expression”)并将数据库设置为“None”的表达式。例如:

String.Format("{0:d}-{1:d}", new DateTime(2012, 1, 6), null)

When you press Run, you'll get the result:

按Run,您将得到结果:

1/6/2012-

#6


You could use the Snippy plugin for Reflector to run little code snippets.

您可以使用Reflector的Snippy插件来运行很少的代码片段。

Looks like the link is dead - just use LinqPad!

看起来链接已经死了 - 只需使用LinqPad!

#7


Just another simple utility, avaliable on MSDN: http://go.microsoft.com/fwlink/?LinkId=209564, description is:

只是另一个简单的实用程序,可在MSDN上使用:http://go.microsoft.com/fwlink/?LinkId = 209564,描述如下:

an application that enables you to apply format strings to either numeric or date and time values and displays the result string.

一个应用程序,使您可以将格式字符串应用于数字或日期和时间值,并显示结果字符串。

But you need to compile it by yourself.

但是你需要自己编译它。

#1


PowerShell works great for testing format strings. From PowerShell you can load your assembly and work with the objects and methods you want to test. You could also just create a string on the command line and test out different formatting options.

PowerShell非常适合测试格式字符串。在PowerShell中,您可以加载程序集并使用要测试的对象和方法。您还可以在命令行上创建一个字符串,并测试不同的格式选项。

You can use the static method from the string class:

您可以使用字符串类中的静态方法:

$teststring = 'Currency - {0:c}.  And a date - {1:ddd d MMM}.  And a plain string - {2}'
[string]::Format($teststring, 160.45, Get-Date, 'Test String')

Or PowerShell has a built in format operator

或者PowerShell具有内置格式运算符

$teststring = 'Currency - {0:c}.  And a date - {1:ddd d MMM}.  And a plain string - {2}'
$teststring -f 160.45, Get-Date, 'Test String'

#2


I just found this:

我刚发现这个:

http://rextester.com/

Simply paste in your format string, and run the code.

只需粘贴您的格式字符串,然后运行代码即可。

It would also be simple enough to create a windows or console project that does exactly that.

创建一个完全相同的窗口或控制台项目也很简单。

#3


Snippet Compiler is a great tool in general for quick small app testing. Instead of cluttering your Visual Studio with a million ConsoleApplication79 projects, just use this. I have it and use it constantly.

Snippet Compiler是一款非常棒的工具,适用于快速小应用测试。不要使用一百万个ConsoleApplication79项目混乱Visual Studio,只需使用它。我有它并且经常使用它。

#4


#5


LinqPad is a great tool that handles this sort of thing brilliantly, even though it's tangential to its primary function (of troubleshooting Linq syntax).

LinqPad是一个出色的工具,可以很好地处理这类事情,即使它与其主要功能(对Linq语法进行故障排除)相关。

Just enter the expression with the language selector set to "C# Expression" (or "VB.net Expression") and the database set to "None." For example:

只需输入语言选择器设置为“C#Expression”(或“VB.net Expression”)并将数据库设置为“None”的表达式。例如:

String.Format("{0:d}-{1:d}", new DateTime(2012, 1, 6), null)

When you press Run, you'll get the result:

按Run,您将得到结果:

1/6/2012-

#6


You could use the Snippy plugin for Reflector to run little code snippets.

您可以使用Reflector的Snippy插件来运行很少的代码片段。

Looks like the link is dead - just use LinqPad!

看起来链接已经死了 - 只需使用LinqPad!

#7


Just another simple utility, avaliable on MSDN: http://go.microsoft.com/fwlink/?LinkId=209564, description is:

只是另一个简单的实用程序,可在MSDN上使用:http://go.microsoft.com/fwlink/?LinkId = 209564,描述如下:

an application that enables you to apply format strings to either numeric or date and time values and displays the result string.

一个应用程序,使您可以将格式字符串应用于数字或日期和时间值,并显示结果字符串。

But you need to compile it by yourself.

但是你需要自己编译它。