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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/426649/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 02:56:42  来源:igfitidea点击:

Looking for a tool to quickly test C# format strings

c#formattingstring

提问by scobi

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 Expressoto 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 等标准类型上试验格式化字符串?

采纳答案by Steven Murawski

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'

回答by RSlaughter

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

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

Looks like the link is dead - just use LinqPad!

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

回答by BFree

Snippet Compileris 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 混乱,只需使用它。我有它并经常使用它。

回答by Graham Charles

LinqPadis 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# 表达式”(或“VB.net 表达式”)且数据库设置为“无”的情况下输入表达式。例如:

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

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

当你按下运行时,你会得到结果:

1/6/2012-

回答by Maxim

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.

但是需要自己编译。

回答by Chris Cudmore

I just found this:

我刚刚发现了这个:

http://rextester.com/

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.

创建一个完全执行此操作的 Windows 或控制台项目也很简单。