Rider Help

Code Analysis and Helpers for String Literals

Rider provides a bunch of features for string literals in your code. You can benefit from Rider's code analysis and numerous helpers in:

Plain strings

Here is how Rider can help you when you are working with plain strings in your code:

  • By default, Rider highlights correct and incorrect escape sequences in all non-verbatim strings:

    Highlighting of escape sequence in a string
  • You can convert regular strings to verbatim strings and back by pressing Alt+Enter when your caret is in the string and choosing the corresponding context actions. This also works with interpolated strings.
  • You can split string literals simply by pressing Enter.
  • If a string contains regular expression, Rider can highlight its syntax and errors, and help you with code completion inside the expression. For more information, see Regular Expressions Assistance.
    Similarly, you can make Rider analyze HTML in string literals. To make Rider aware of the specific language inside a string literal, you can either use the corresponding context actions or add a comment /*language=regexp|jsregexp|html*/ right before the string literal.
  • You can select some substring within a string and automatically introduce a variable for the substring.

String formatting methods

Rider analyzes format strings and arguments of all.NET string formatting methods, such as String.Format, Text.StringBuilder.AppendFormat, or Console.WriteLine.

In usages of string formatting methods, Rider highlights format placeholders and also synchronously highlights placeholder with the corresponding argument when your caret is at either of them:

Highlighting of arguments and placeholders in string formatting methods

If arguments and format placeholders mismatch (which leads to the FormatException in runtime if arguments are missing), Rider generates warnings for missing or redundant arguments:

Warning for missing arguments in string formatting methods

You can easily fix this problem by pressing Alt+Enter over the warning. Rider will suggest either to automatically add the missing argument or to remove the mismatched format placeholder.

Rider also helps you detect and remove redundant calls of string formatting methods inside other string formatting methods. For example:

Redundant call to string formatting method

To quickly convert a concatenation of string literals and variables, press Alt+Enter anywhere within the concatenation and use the To String.Format invocation context action. For example, if we apply this context action in the return statement of the following method:

public string Greet(string name, int age) { return "Hi, my name is " + name + " and I'm " + age; }

It will be converted to:

public string Greet(string name, int age) { return String.Format("Hi, my name is {0} and I'm {1}", name, age); }

You can also use context actions to automatically add and remove format placeholders and arguments in strings. When you invoke the Insert format argument action within a string literal, Rider inserts a new placeholder with the proper index and brings you to the position where you can immediately start typing the argument. This action can also be invoked on a plain string. In this case Rider will automatically convert it to the String.Format call.

In order to quickly remove a format placeholder together with the corresponding argument, set the caret at the placeholder, press Alt+Enter and choose Remove format argument.

Custom string formatting methods

When a custom string formatting method appears, you have to tell Rider to interpret it as such, which is quite easy - you need to decorate the method with the [StringFormatMethodAttribute] from the JetBrains.Annotations namespace. As soon as the method has this attribute, you will enjoy all features available for standard formatting methods.

Consider a custom string formatting method ShowError:

public void ShowError(string formatString, params object[] args) { // some custom logic Console.WriteLine(formatString, args); }

If the method is called incorrectly, as shown below, Rider has no chance to detect the missing argument:


To make Rider aware of a custom string formatting method

  1. Reference the JetBrains.Annotations namespace as described in the Annotations in Source Code section.
  2. Annotate your custom string formatting method with the [StringFormatMethodAttribute] attribute, which takes a single argument - the name of the format string parameter:
    [StringFormatMethod("formatString")] public void ShowError(string formatString, params object[] args) { // some custom logic Console.WriteLine(formatString, args); }

Rider will now be able to warn you about missing arguments when this custom formatting method is invoked. Even more, the Add argument quick-fix will make it easier for you to insert them:

Quick-fix for the string formatting method call

Interpolated strings

If you are using C# 6.0 in your project, you can use interpolated strings instead of the String.Format method.

By default, Rider automatically detects C# version based on the associated compiler. However, you can specify the target C# version explicitly — press Alt+Enter on the project in the Solution Explorer and use the Language level selector on the Application page of the Project Properties dialog..

In C# 6.0 projects, Rider highlights usages of String.Format with the suggestion to convert them into interpolated strings. You can use a quick-fix to convert the current usage or all usages in the specific scope with a couple of keystrokes.

Converting a usage of String.Format into string interpolation

If an interpolated string contains no expressions, Rider highlights the $ sign as redundant and helps you remove it:

Converting string interpolation without parameters into string literal
Last modified: 11 October 2017

See Also