Code analysis and helpers for string literals
Here is how JetBrains Rider can help you when you are working with plain strings in your code:
By default, JetBrains Rider highlights correct and incorrect escape sequences in all non-verbatim strings:
You can convert regular strings to verbatim strings and back by pressing AltEnter 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, JetBrains Rider can highlight its syntax and errors, and help you with code completion inside the expression. For more information, refer to Regular expressions assistance.
Similarly, you can make JetBrains Rider analyze HTML in string literals. To make JetBrains 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.
If your project is localizable, you can automatically move a string to resource.
JetBrains 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, JetBrains 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 Highlighting of arguments and placeholders in string formatting methods](https://resources.jetbrains.com/help/img/rider/2024.3/string_formatting_highlight.png)
If arguments and format placeholders mismatch (which leads to the FormatException
in runtime if arguments are missing), JetBrains Rider generates warnings for missing or redundant arguments:
![Warning for missing arguments in string formatting methods Warning for missing arguments in string formatting methods](https://resources.jetbrains.com/help/img/rider/2024.3/string_formatting_warning.png)
You can easily fix this problem by pressing AltEnter over the warning. JetBrains Rider will suggest either to automatically add the missing argument or to remove the mismatched format placeholder.
JetBrains 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 Redundant call to string formatting method](https://resources.jetbrains.com/help/img/rider/2024.3/string_formatting_redundant.png)
To quickly convert a concatenation of string literals and variables, press AltEnter 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, JetBrains 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 JetBrains Rider will automatically convert it to the String.Format
call.
In order to quickly remove a format placeholder together with the corresponding argument, place the caret at the placeholder, press AltEnter and choose Remove format argument.
To enable code analysis and assistance features in custom string formatting methods, use the [StringFormatMethod]
and [StructuredMessageTemplate]
attributes from the JetBrains.Annotations namespace.
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, JetBrains Rider has no chance to detect the missing argument:
![String formatting method called incorrectly String formatting method called incorrectly](https://resources.jetbrains.com/help/img/rider/2024.3/stringFormattingMethods_01.png)
Reference the
JetBrains.Annotations
namespace as described in the Annotations in source code section.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); }
JetBrains Rider will 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:
Alternatively, annotate the parameter that accepts a formatted string with the
[StructuredMessageTemplateAttribute]
:void LogNewUser([StructuredMessageTemplate] string message, params string[] args) { // Log new user } void Test() { // Warning: Non-existing argument in format string LogNewUser("User created: {username}"); }
This second approach allows you to use custom strings as placeholders, as
username
in the example above.
tip
You can annotate custom string formatting methods in compiled libraries using external annotations.
Modern versions of C# provide a more elegant alternative to the String.Format
method — interpolated strings. That's why JetBrains Rider highlights usages of String.Format
with the suggestion to convert them into interpolated strings. You can use a quick-fix to apply the conversion for the current usage or for all usages in the specific scope with a couple of keystrokes.
![JetBrains Rider: Converting a usage of String.Format to string interpolation JetBrains Rider: Converting a usage of String.Format to string interpolation](https://resources.jetbrains.com/help/img/rider/2024.3/use_string_interpolation.png)
If an interpolated string contains no expressions, JetBrains Rider highlights the $
sign as redundant and helps you remove it:
![JetBrains Rider: Converting string interpolation without parameters into string literal JetBrains Rider: Converting string interpolation without parameters into string literal](https://resources.jetbrains.com/help/img/rider/2024.3/to_string_literal_fix.png)
If you need to quickly add the $
at the beginning of the string when you are deep into typing out the string, you can press AltEnter and choose To string interpolation.
You can then also choose Insert interpolation argument and JetBrains Rider will add the argument placeholder { }
at your caret position.
![JetBrains Rider: Converting simple string to string interpolation JetBrains Rider: Converting simple string to string interpolation](https://resources.jetbrains.com/help/img/rider/2024.3/ca_to_string_interpolation.png)