3

I want to convert all strings that have a variable in it, example:

string = "Hi " + name + ", How are you?"

to his interpolated version, refactoring all these cases in a class.

I tried several ways with visual studio and visual studio code, but no one has this function, maybe with regex i can achieve this.

4
  • Are you asking for some automated way to turn "Hi " + name + ", How are you?" into $"Hi {name}, How are you?"? Commented Jan 13, 2023 at 12:00
  • Exactly, but for all ocurrences in a class. Commented Jan 13, 2023 at 12:05
  • Weird. There's an option to apply "Convert to raw string" to everything in the document, but "Convert to interpolated string" is only available for a single instance at a time Commented Jan 13, 2023 at 12:06
  • @agmalo118 ,If Matthew Watson's answer solved your issue, you should mark it as the answer. Commented Jan 16, 2023 at 1:48

1 Answer 1

4

Visual Studio can only do this for one instance at a time, as you have probably already discovered.

However, Resharper has a function to do this for all occurrences.

For the following code:

public static void Main()
{
    string name = "matthew";

    string s = "Hi " + name + ", How are you?";
    string t = "Hi " + name + ", How are you?";

    Console.WriteLine(s + t);
}

It provides something like this:

Resharper refactoring options

The options for "Method" and "Class" only appear if they are needed, so this screenshot was generated from slightly different code than shown above.

Sign up to request clarification or add additional context in comments.

3 Comments

Though it doesn't support the exact requirement OP is asking for "in a class" (assuming multiple classes in the same file)
Maybe my comment is incorrect, as your linked page, shows a screenshot with a class option. blog.jetbrains.com/wp-content/uploads/2022/02/…
@RandRandom It only shows options for "Method" and "Class" if there is more than one method or class in the file to which the refactoring could be applied. I'll update my answer with a screenshot showing all the options.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.