Drag string.format

I often write

var message = "Hello {0}";

and then go back and type

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

It would be nice if I typed a string when I put "at the end", the regarder realized that there was a parameter in the string, and immediately surrounded the string with string.Format and placed the cursor in the first argument.

Is there an easy way to do this? I am using Resharper 6.1

+5
source share
5 answers

Just enter it in dumber:

 "Hello " + world

Alt+Enter Enterdone 1 :

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

Obviously, this also works when all this is much more complicated. I know that it will disable useless calls .ToString(), and I suspect that it will automatically pick up any format expressions, like

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

Alt+Enter Enter

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

1 / , Resharper (?), +

+16

, , , , a-la PHP:

enter image description here

ReSharper, .

+3

FormatWith(arg0, ar1...). , Humanizer . Humanizer NuGet, "Heres my formatted string on the {0}st try!".FormatWith(1)", , . ReSharper, , Humanizer Annotations R # Extension, .

+2

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}"

, Ctrl K Ctrl S , .

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

value, .

: , .

+1

Visual Studio Matthew. , , , .

:

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>
+1

All Articles