Visual Studio Find and Replace change casing - c#

I'm working in a group that's converting an old WinForms app to a WPF app, using the MVVM pattern. Thus, we're having to write a lot of large models. I thought I'd write a find-and-replace regex so we can write our private variables:
private string myString;
and do a find-and-replace across the large file and get:
private string myString;
public string MyString
{
get { return myString; }
set { SetProperty("MyString", ref myString, value); }
}
this isn't too bad, the only issue I have is converting myString to MyString.
Is there a way, using straight regex capture groups, to replace a character with it's uppercase version, within Visual Studio's find-and-replace? All my searches just turn up using C# code to do the conversion, which obviously isn't possible in this context.

When I come across this issue, the sometimes fastest way is to replace everything with something completely different at first like "MyNOTString", and then back to "MyString" to get the right capital letters. It does not seem that you can get what you are asking for in Visual Studio without using Macros and such.
Another solution would be to open all the project files in a different tool like Notepad++, which supports writing /U to turn something into uppercase. See this answer.

Related

How does Visual Studio syntax-highlight strings in the Regex constructor?

Hi fellow programmers and nerds!
When creating regular expressions Visual Studio, the IDE will highlight the string if it's preceded by a verbatim identifier (for example, #"Some string). This looks something like this:
(Notice the way the string is highlighted). Most of you will have seen this by now, I'm sure.
My problem: I am using a package acquired from NuGet which deals with regular expressions, and they have a function which takes in a regular expression string, however their function doesn't have the syntax highlighting.
As you can see, this just makes reading the Regex string just a pain. I mean, it's not all-too-important, but it would make a difference if we can just have that visually-helpful highlighting to reduce the time and effort one's brain uses trying to decipher the expression, especially in a case like mine where there will be quite a quantity of these expressions.
The question
So what I'm wanting to know is, is there a way to make a function highlight the string this way*, or is it just something that's hardwired into the IDE for the specific case of the Regex c-tor? Is there some sort of annotation which can be tacked onto the function to achieve this with minimal effort, or would it be necessary to use some sort of extension?
*I have wrapped the call to AddStyle() into one of my own functions anyway, and the string will be passed as a parameter, so if any modifications need to be made to achieve the syntax-highlight, they can be made to my function. Therefore the fact that the AddStyle() function is from an external library should be irrelevant.
If it's a lot of work then it's not worth my time, somebody else is welcome to develop an extension to solve this, but if there is a way...
Important distinction
Please bear in mind I am talking about Visual Studio, NOT Visual Studio Code.
Also, if there is a way to pull the original expression string from the Regex, I might do it that way, since performance isn't a huge concern here as this is a once-on-startup thing, however I would prefer not to do it that way. I don't actually need the Regex object.
According to https://devblogs.microsoft.com/dotnet/visual-studio-2019-net-productivity/#regex-language-support and https://www.meziantou.net/visual-studio-tips-and-tricks-regex-editing.htm you can mark the string with a special comment to get syntax highlighting:
// language=regex
var str = #"[A-Z]\d+;
or
MyMethod(/* language=regex */ #"[A-Z]\d+);
(the comment may contain more than just this language=regex part)
The first linked blog talks about a preview, but this feature is also present in the final product.
.NET 7 introduces the new [StringSyntax(...)] attribute, which is used in .NET 7 on more than 350 string, string[], and ReadOnlySpan<char> parameters, properties, and fields to highlight to an interested tool what kind of syntax is expected to be passed or set.
https://devblogs.microsoft.com/dotnet/regular-expression-improvements-in-dotnet-7/?WT_mc_id=dotnet-35129-website&hmsr=joyk.com&utm_source=joyk.com&utm_medium=referral
So for a method argument you should just use:
void MyMethod([StringSyntax(StringSyntaxAttribute.Regex)] string regex);
Here is a video demonstrating the feature: https://youtu.be/Y2YOaqSAJAQ

Visual Studio 2015 'use var' refactoring shortcut

Is there a shortcut for refactoring a line of code to use the var keyword similar to what ReSharper offers?
For example if you have the following piece of code:
public void Foo()
{
List<Tuple<int, object>> bar = new List<Tuple<int, object>>();
}
ReSharper can format it into:
public void Foo()
{
var bar = new List<Tuple<int, object>>();
}
Visual Studio does not have a direct equivalent to many of the ReSharper's refactoring tools. Unfortunately, "use var" is one of the missing refactoring features, at least as of VS 2015.
You can use this regex as poor man's refactoring. Press Ctrl+H, then type
Search: (^\s*)\S[^=()]* (\w+\s*=)
Replace: $1var $2
The "\S[^=()]* " portion, including the space, matches the declaration, the ^\s* matches indentation, and \w+\s*= matches variable name and = in front of initialization.
Indentation is captured into $1 group; variable and = are captured into $2. Replacement string uses the two captured strings to format a replacement with var in the middle.
This trick can help you convert the entire file pretty quickly: visually confirm that you want to apply "refactoring" to the selected item, then press → button to go to the next declaration.
Although this approach lends itself to making a macro with relative ease, built-in support for macros has been dropped from Visual Studio as of VS 2010, so creating a quick shortcut manually for this type of refactoring would require installation of an external macro tool as well.
While there is no direct way to do this now, support for this in Visual Studio appears to be on the way in a future release of Rosalyn.
In addition to #dasblinkenlight's answer, I would keep an eye out for any custom NuGet code analyzers that do this. If it shows up as a green-squiggly error, you can effectivly get your shortcut via a quick action: Ctrl+..
I haven't been able to find one yet (besides one that does exactly the opposite of what you're looking for).

how to make string returned by ResourceManager.GetString not verbatim

Okay, I have a string
string textToShow = "this\nrocks"
which when put in label in winforms window will then show
this
rocks
Which is the result I'd like to get. Now, instead of setting the textToShow in the code, I set it in the resource file. When I tried to get the value from resource file using
Properties.Resources.ResourceManager.GetString("textToShow");
the whole string instead will be treated as verbatim, showing
this\nrocks
when put in a label in a winforms window. This is not the result i'm looking for. What's the best way to store strings with special characters in resource file then? I can do string replace for every special characters, like
string.Replace(#"\n", "\n");
but then I need to replace every special characters whenever I call method ResourceManager.GetString, which I think is not the most elegant solution. If there is some ways to make string returned from method ResourceManager.GetString not verbatim, please do tell me.
Thanks
This was already answered here: StackOverflow: How to deal with newline
Basically you have two useful options:
Use shift + enter in the resource manager text editer to add a new line.
Or use String.Format() to replace {0} with \n on read.
The .Net 4.5 framework has the unescape functionality as shown here:
using System.Text.RegularExpressions;
Regex.Unescape(Properties.Resources.ResourceManager.GetString("textToShow"));
solves your issue. Now you can use \n and \u in the resource files.
On the resource editor type "this<shift+enter>rocks" as the resource value.

Find a specific string within another string without getting similar results

In a console application I get as input the RTF (Rich text Format) code of a file. The source is a database and data gathered via query.
My goal is to search whether in the input code, as string, is present the code: \par (end of carriage in RTF).
I tried with string.IndexOf and string.Contains but both returns me bad results since they match also code like: "\pard".
Given a string like:
{\rtf1\ansi\deff0{\fonttbl{\f0\fnil\fcharset0 Times New Roman;}
{\f1\fnil\fcharset0 MS Sans Serif;}
\deflang1033\pard\plain\tx0\f2\lang1033\fs20\cf1 Payment}
How can I build my condition so that it return false, since the string does not contain \par? Eventually how could I set a regex to say that exactly the keyword "\par" (so length 4 chars) and no other will match? Thanks.
EDIT: The language used is C# and I am developing the console application with VS 2010.
You don't tell us the language you are using, but generally you need a word boundary something like this:
\\par\b
to ensure that there is not a word character following

split a string using a open and close tag

I wish to known if exist a clean way to split a string using different tags for opening and ending.
For example:
<&field1&>outside<&field2&>
using the function split:
string[] dd={"<&","&>"};
string[] b1 = a1.Split(dd,StringSplitOptions.None);
it show me:
0:
1:field1
2:outside
3:field2
4:
(that it is that i want to do).
but also
<&field1<&outside<&field2<&
show the same.
#"\G<&(?<code>.*?)&>"
The TemplateParser in the AspCodeRegex class in System.Web.RegularExpressions uses something similar to this(answer via #rexm)
You should use a regular expression to do this. After a quick play I came up with this which seems to match the entries within the <& &> delimiters, but you get the idea:
<&([^&]*)&>
See Regular Expression Examples for some more examples and also the code you need to run your regex.

Categories

Resources