Resharper string.format shortcut

前端 未结 5 1111
礼貌的吻别
礼貌的吻别 2021-01-13 04:04

I often find myself writing

var message = \"Hello {0}\";

and then going back and typing in

var message = string.Format(\"He         


        
相关标签:
5条回答
  • 2021-01-13 04:46

    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

    0 讨论(0)
  • 2021-01-13 04:46

    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.

    0 讨论(0)
  • 2021-01-13 04:47

    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.

    0 讨论(0)
  • 2021-01-13 04:50

    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>
    
    0 讨论(0)
  • 2021-01-13 04:55

    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.

    0 讨论(0)
提交回复
热议问题