Resharper string.format 快捷方式
我经常发现自己在写
var message = "Hello {0}";
,然后返回并输入。
var message = string.Format("Hello {0}", world);
如果当我输入字符串时,当我将 " 放在末尾时,resharper 发现字符串中有一个参数,并立即用string.Format 方法并将光标放在第一个参数上,
有没有一种简单的方法可以做到这一点?我正在使用 Resharper 6.1
I often find myself writing
var message = "Hello {0}";
and then going back and typing in
var message = string.Format("Hello {0}", world);
It would be nice if as I were typing the string, when I put the " at the end, resharper figured out there was a parameter in the string and immediately surrounded the string with the string.Format method and put the cursor at the first argument.
Is there a straightforward way to do this? I'm using Resharper 6.1
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
只需在哑巴中输入:
Alt+EnterEnter,完成1:
显然,当整个事情更加复杂时,这也适用。我知道它会删除对
.ToString()
无用的调用,并且我怀疑它会自动提升任何格式表达式,例如Alt+EnterEnter
1 如果您不走运/周围的代码包含许多触发 Resharper 建议的内容(?),您可能必须将光标放在
+
运算符之一上Just type it in dumber:
Alt+EnterEnter, done1:
Obviously, this also works when the whole thing is much more complex. I know it will strip useless calls to
.ToString()
, and I suspect it will automatically lift any format expressions, likeAlt+EnterEnter
1 If you're unlucky/the surrounding code contains many things that trigger Resharper suggestions(?) you might have to position the cursor over one of the
+
operators无耻插件
我还尝试制定一种方法来使字符串格式创建更容易,我想到的是字符串拼接 a-la PHP:
这是 ReSharper 插件的一部分,您可以在 此处。
Shameless plug
I've also tried to formulate an approach to make string format creation easier, and what I came up with is string splicing a-la PHP:
This is part of a ReSharper plug-in that you can find here.
因此,我最终为名为
FormatWith(arg0, ar1...)
的字符串编写了一个扩展方法。然后我发现 Humanizer 库做了同样的事情。添加 Humanizer NuGet 包,现在您将能够编写"Heres my formatted string on the {0}st try!".FormatWith(1)"
,希望能减少跳动。如果您有ReSharper 并像它突出显示与参数匹配的地标一样,安装 Humanizer注解 R# 扩展,您将得到它们。I ended up writing an extension method for strings named
FormatWith(arg0, ar1...)
because of this. Then I found that the Humanizer library did the same exact thing. Add the Humanizer NuGet package and now you'll be able to write"Heres my formatted string on the {0}st try!".FormatWith(1)"
with hopefully less bouncing around. If you have ReSharper and like that it highlights the matching placemarkers with the parameter, install the Humanizer Annotations R# Extension and you'll get them back.您几乎可以使用 Visual Studio 代码片段(即不使用 ReSharper)来完成此操作。
将以下内容保存为扩展名为
.snippet
的文件。然后您可以通过工具|加载它。代码片段管理器 |导入。
代码段可用后,您可以键入,
但必须选择字符串,然后按 CtrlK CtrlS< /kbd> 并选择代码片段名称以应用它。这将
使用选择进行编辑的
value
部分生成。编辑:还有一个代码段设计器扩展 这使得使用片段变得更容易。
You can almost do this with a Visual Studio snippet (i.e. without ReSharper).
Save the following as a file with a
.snippet
extension.Then you can load it via Tools | Code Snippets Manager | Import.
Once the snippet is available, you can type
but you'd have to select the string and then press CtrlK CtrlS and select the snippet name to apply it. This would generate
with the
value
part selected for editing.Edit: There's also a Snippet Designer extension that makes working with snippets easier.
这是 Matthew 的 Visual Studio 代码片段的替代代码。此代码片段要求提供变量名称,但默认为消息,该消息是可选的,唯一需要的是变量名称。 HTH
看起来像:
作为默认值(abc {0} 是突出显示的文本)
Here is an alternate to Matthew's visual studio code snippet. This snippet asks for the variable name but defaults to message which is optional an the only thing required is the variable name. HTH
Looks like:
as the default (the abc {0} was the highlighted text)