Resharper string.format 快捷方式

发布于 2024-12-23 13:15:17 字数 295 浏览 1 评论 0原文

我经常发现自己在写

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

糖粟与秋泊 2024-12-30 13:15:17

只需在哑巴中输入:

 "Hello " + world

Alt+EnterEnter,完成1

 string.Format("Hello {0}", world);

显然,当整个事情更加复杂时,这也适用。我知道它会删除对 .ToString() 无用的调用,并且我怀疑它会自动提升任何格式表达式,例如

 int i = 42;
 "i = " + i.ToString("X2"); 

Alt+EnterEnter

 string.Format("i = {0:X2}", i);

1 如果您不走运/周围的代码包含许多触发 Resharper 建议的内容(?),您可能必须将光标放在 + 运算符之一上

Just type it in dumber:

 "Hello " + world

Alt+EnterEnter, done1:

 string.Format("Hello {0}", world);

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, like

 int i = 42;
 "i = " + i.ToString("X2"); 

Alt+EnterEnter

 string.Format("i = {0:X2}", i);

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

吃→可爱长大的 2024-12-30 13:15:17

无耻插件

我还尝试制定一种方法来使字符串格式创建更容易,我想到的是字符串拼接 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:

enter image description here

This is part of a ReSharper plug-in that you can find here.

简单 2024-12-30 13:15:17

因此,我最终为名为 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.

北笙凉宸 2024-12-30 13:15:17

几乎可以使用 Visual Studio 代码片段(即不使用 ReSharper)来完成此操作。

将以下内容保存为扩展名为 .snippet 的文件。

<?xml version="1.0" encoding="utf-8"?>
<CodeSnippet Format="1.0.0" xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
    <Header>
        <Title>string format</Title>
        <Author>Matthew Strawbridge</Author>
        <Description>Wraps the selected text with string.Format</Description>
        <SnippetTypes>
            <SnippetType>SurroundsWith</SnippetType>
        </SnippetTypes>
    </Header>
    <Snippet>
        <Declarations>
            <Literal>
                <ID>variable</ID>
                <Default>value</Default>
            </Literal>
        </Declarations>
        <Code Language="CSharp">
            <![CDATA[string.Format($selected$, $variable$);]]>
        </Code>
    </Snippet>
</CodeSnippet>

然后您可以通过工具|加载它。代码片段管理器 |导入。

代码段可用后,您可以键入,

var message = "Hello {0}"

但必须选择字符串,然后按 CtrlK CtrlS< /kbd> 并选择代码片段名称以应用它。这将

var message = string.Format("Hello {0}", value);

使用选择进行编辑的 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.

<?xml version="1.0" encoding="utf-8"?>
<CodeSnippet Format="1.0.0" xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
    <Header>
        <Title>string format</Title>
        <Author>Matthew Strawbridge</Author>
        <Description>Wraps the selected text with string.Format</Description>
        <SnippetTypes>
            <SnippetType>SurroundsWith</SnippetType>
        </SnippetTypes>
    </Header>
    <Snippet>
        <Declarations>
            <Literal>
                <ID>variable</ID>
                <Default>value</Default>
            </Literal>
        </Declarations>
        <Code Language="CSharp">
            <![CDATA[string.Format($selected$, $variable$);]]>
        </Code>
    </Snippet>
</CodeSnippet>

Then you can load it via Tools | Code Snippets Manager | Import.

Once the snippet is available, you can type

var message = "Hello {0}"

but you'd have to select the string and then press CtrlK CtrlS and select the snippet name to apply it. This would generate

var message = string.Format("Hello {0}", value);

with the value part selected for editing.

Edit: There's also a Snippet Designer extension that makes working with snippets easier.

他是夢罘是命 2024-12-30 13:15:17

这是 Matthew 的 Visual Studio 代码片段的替代代码。此代码片段要求提供变量名称,但默认为消息,该消息是可选的,唯一需要的是变量名称。 HTH

看起来像:

var message = string.Format( "abc {0}", variable ); 

作为默认值(abc {0} 是突出显示的文本)

<?xml version="1.0" encoding="utf-8"?>
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
  <CodeSnippet Format="1.0.0">
    <Header>
      <Title>String Format</Title>
      <Author>OmegaMan</Author>
      <Description>Surrounded text gets format</Description>
      <HelpUrl></HelpUrl>
      <SnippetTypes />
      <Keywords />
      <Shortcut>#SF</Shortcut>
    </Header>
    <Snippet>
      <References />
      <Imports />
      <Declarations>
        <Literal Editable="true">
          <ID>name</ID>
          <Type></Type>
          <ToolTip>What the variable name should be.</ToolTip>
          <Default>message</Default>
          <Function></Function>
        </Literal>
        <Literal Editable="true">
          <ID>Vars</ID>
          <Type></Type>
          <ToolTip>The target variable for format.</ToolTip>
          <Default>variable</Default>
          <Function></Function>
        </Literal>
      </Declarations>
      <Code Language="csharp" Kind="" Delimiter="$"><![CDATA[var $name$ = string.Format($selected$, $Vars$);$end$ ]]></Code>
    </Snippet>
  </CodeSnippet>
</CodeSnippets>

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:

var message = string.Format( "abc {0}", variable ); 

as the default (the abc {0} was the highlighted text)

<?xml version="1.0" encoding="utf-8"?>
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
  <CodeSnippet Format="1.0.0">
    <Header>
      <Title>String Format</Title>
      <Author>OmegaMan</Author>
      <Description>Surrounded text gets format</Description>
      <HelpUrl></HelpUrl>
      <SnippetTypes />
      <Keywords />
      <Shortcut>#SF</Shortcut>
    </Header>
    <Snippet>
      <References />
      <Imports />
      <Declarations>
        <Literal Editable="true">
          <ID>name</ID>
          <Type></Type>
          <ToolTip>What the variable name should be.</ToolTip>
          <Default>message</Default>
          <Function></Function>
        </Literal>
        <Literal Editable="true">
          <ID>Vars</ID>
          <Type></Type>
          <ToolTip>The target variable for format.</ToolTip>
          <Default>variable</Default>
          <Function></Function>
        </Literal>
      </Declarations>
      <Code Language="csharp" Kind="" Delimiter="$"><![CDATA[var $name$ = string.Format($selected$, $Vars$);$end$ ]]></Code>
    </Snippet>
  </CodeSnippet>
</CodeSnippets>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文