views:

24

answers:

2

I have a large ASP.NET project where I want to do a mass search and replace (about 3500 instances)

I want to change

If strErrorMessage.Length > 0

If strSomeString.Length > 0

If strWhatever.Length > 0

and any other similar call to the Length method from a string to the following

If Len(strErrorMessage) > 0

If Len(strSomeString) > 0

If Len(strWhatever) > 0

Anyway to reliably do this in one shot?

I can do a search and replace for something like

If *.Length > 0 --> If Len(*) > 0 

This just won't work though as it won't understand how to rearrange it properly. Currently have VS2010 and N++ at my disposal.

Any ideas?

+1  A: 

About Ahmed's comment. The likely reason here is that the length method is an instance method and as such, if invoked on a string variable that is null, will error.

LEN on the other hand, is more like an extension method and will simply return a 0 whether the string is null or a string that contains no chars. Len works much more like len should work in my opinion.

But back to the question.

Have you checked out

http://rxfind.codeplex.com/

A regex command line search and replace tool. That should make quick work of that job.

drventure
+1 good response about `Len()` versus `.Length`. Thanks for pointing that out.
Ahmad Mageed
+1  A: 

Using the Visual Studio Find/Replace (with the regex options enabled) you can use this:

Find what: If {:a+}\.Length \> 0

Replace with: If Len(\1) \> 0

Pattern explanation:

  • :a+ = the :a matches an alphanumeric char, and the + matches at least one occurrence
  • {} in {:a+} = Visual Studio regex's way of "tagging" (i.e., capturing) an expression
  • \> = the > must be escaped with a backslash since it's a metacharacter in this regex flavor.
  • \1 = refers to the text matched in the tagged expression. The number 1 refers to the first (and only, in this case) tagged expression.

You can read more about the MSDN regex reference for Find/Replace here.

As I had mentioned in my comment, I think using Len() is a step backwards and ties your code down to the Microsoft.VisualBasic namespace. @drventure brought up a good point though, since calling .Length on a null value would throw an exception. Instead of checking the length you could use String.IsNullOrEmpty. In .NET 4.0 you can also use String.IsNullOrWhiteSpace.

Instead of If strErrorMessage.Length > 0 you can use:

If Not String.IsNullOrEmpty(strErrorMessage) Then
' or '
If Not String.IsNullOrWhiteSpace(strErrorMessage) Then

If you're interested in using this you can keep the original "Find what" pattern and change the "Replace with" pattern to this: If Not String.IsNullOrEmpty(\1)

Ahmad Mageed
There's really no such thing as "tying your code down to the VisualBasic namespace". That dll is part of the .net framework core and isn't likely to be going anywhere anytime soon. Plus there's plenty of examples of C# code making use of functions within that namespace to simplify various pieces of functionality.
drventure
That said, Ahmed's point about String.IsNullOrEmpty is still a good one. It's effectively the same thing as Len(x) = 0, even though it's always bugged me as being awfully wordy. Of course, a nice little extension method on the string class could take care of that.
drventure