Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find and Replace on Visual studio with keeping the number

Is there way on visual studio to find and replace text but keeping the number in the string same?

For example, lets say I have a code that saids

fields[0].Value;
fields[1].Value;

And now I would like to replace it with

reader.GetString(0);
reader.GetString(1);

Without manually replacing every single lines of code, I was hoping to do it through find and replace dialog.

Is there any ways of doing this?

Thank you

like image 363
Shintaro Takechi Avatar asked Mar 15 '23 07:03

Shintaro Takechi


1 Answers

If you want to replace part of the expression but keep a part (like the number in your case) you can use the search and replace function (ctrl+h) set to use regular expressions (alt+e) and use these expressions:

Search: fields\[(.)\].Value;

Replace: reader.GetString($1);

This will replace all expressions on the form fields[n].Value; with reader.GetString(n); where n is any single character. If you want to restrict it to keep numbers only use fields\[(\d)\].Value;

For more information see: Using Regular Expressions in Visual Studio

I tried it with VS2013 and it worked as expected.

like image 81
jpw Avatar answered Apr 06 '23 09:04

jpw