Slicing and Dicing Strings with CodeRush for Visual Studio

CodeRush brings powerful string manipulation features to Visual Studio. Here are a few of my favorites:

Splitting Strings

When you need to break a string literal into two or three pieces. For example, let’s start with the following text:

 

public string GetGreetingText(int daysLeft)
{
  return "Welcome back, customer. \nSo glad to see you. Days remaining on our special offer: " + daysLeft.ToString();
}

I can place the caret (the flashing I-beam that marks the editor’s insertion point) right before “Days remaining” text and press the CodeRush/Refactor! key (Ctrl+` by default) and then choose Split String.

SplitStringInto2

This will split the string into two pieces.

Next, let’s select the last two expressions on this line:

SelectLastExpression

Press the CodeRush/Refactor! key, and choose Extract Method to create a new method called GetDaysRemaining, like this:

GetDaysRemaining

Use String.Format

Next, move the caret down inside the string inside the GetDaysRemaining method. Press the CodeRush/Refactor! key and select Use String.Format.

UseStringFormat

This neatly combines the concatenation into a single string that is easy to read and so much easier to translate (translating string fragments is much harder to do when grammatical rearrangements are needed as part of the localization).

Speaking of translation…

Extract String to Resource

Move the caret inside the “Days remaining…” string. Press the CodeRush/Refactor! key and select Extract String to Resource, then select “Create new resource file”.

ExtractStringToResource

CodeRush will create a new resource file to hold this string, and highlight the resource identifier for an easy rename (tip: I like to use Camel Case Nav and Camel Case Select – Alt plus Left or Right arrow keys with Shift to select -- to quickly trim a long identifier down to a reasonable size). Let’s call this resource “DaysRemaining”.

DaysRemainingResource

Placing strings in resource files is a good first step to preparing your code for translation.

Splitting Strings, Revisited

The CodeRush Split String refactoring has a bonus feature built in: the ability to split a string into three pieces. Just select the portion of the string that you want to pull out and apply the refactoring. For example, in the code above, there is a “\n” appearing inside the string. This “\n” may be confusing to translators, it makes the string harder to read, and it may not be the most appropriate way to terminate lines on some platforms, so let’s take it out and replace it with the Environment.NewLine expression.

First, select the “\n”:

SelectSlashN

Press the CodeRush/Refactor! key and select Split String

ApplySplitStringIntoThree

Applying the refactoring will leave you with this:

SplitStringInto2Applied

Notice the “\n” string fragment is selected for easy editing. Now we can use CodeRush’s “enl” template to replace the “\n” with a proper Environment.NewLine expression.

AfterEnvironmentNewLine

Introduce Format Item

See that “Welcome back, customer.” string in the code above? It would be great if we could actually pass in the customer’s name in place of the text “customer”. That would make the greeting certainly more personal. It’s easy to do this with CodeRush. Just select the part of the string that you want to replace, and press the CodeRush/Refactor! key, like this:

IntroduceFormatItemPreviewHint

Apply the Introduce Format Item refactoring to get this:

AppliedIntroduceFormatItem

Notice that once again the extracted string is selected for easy editing. Now let’s promote this string to a parameter. Press the CodeRush/Refactor! key and select Promote to Parameter.

PromoteToParameter

And give the new parameter a meaningful name, like customerName:

PromoteToParameterAfterRename

Converting Text to Strings

Sometimes you need to work with a text coming from another environment, and that text span several lines or contain quote characters (that would need to be escaped). So in this example I used the “ms” template to create a method that returns a string, and I expanded the “r” template to get the return keyword and then I pasted in some XAML code that creates a TextBlock:

private string GetTextBlock()
{
  return <TextBlock
  Canvas.Left="25" Canvas.Top="5"
 
Foreground="Teal" FontFamily="Verdana" FontSize="18" FontWeight="Bold"
 
Text="Sample Output" />;
}

Of course this code will not compile. Fortunately we have CodeRush installed so getting it into its desired shape is easy. Just select the text we want to convert, press the CodeRush/Refactor! key and select Embed Selection | To string, like this:

EmbedString

CodeRush will convert the text into a string, like this:

ConvertToString

Nice. Notice how the quotes are properly escaped and the separate lines are now concatenated together.

Now, suppose you wanted to parameterize some of the values in this string. For example, turn the “Sample Output” into a “text” parameter. How would you do that? Well, you should already know how. Just select the text, split the string, and promote it to a parameter.

Wrapping Up

If you’re not already using CodeRush, you should be. This post has covered only a portion of the tools and features that work with strings, and working with strings is only a very small tip of the very large iceberg of power that is CodeRush. CodeRush is the fastest, most powerful, and most memory-efficient developer productivity add-on available for Visual Studio.

Free DevExpress Products - Get Your Copy Today

The following free DevExpress product offers remain available. Should you have any questions about the free offers below, please submit a ticket via the DevExpress Support Center at your convenience. We'll be happy to follow-up.
No Comments

Please login or register to post comments.