On GameSpot: Wii Fit tells 10-year-old she's fat

Replace all occurrences of a pattern in a string with VB.NET

Tags: Microsoft development tools, Programming languages, .NET, Irina Medvinskaya, Regex.Replace, Microsoft Visual Basic.Net, Developer, TechRepublic Inc., Newsletter, IBM Lotus Notes, String, Text, Regex.Replace Method, Software Development, Software/Web Development, Visual Basic Tips Newsletter

  • Save
  • Print
  • 0

Takeaway: The Regex.Replace method allows you to replace a pattern in a string with a fixed string literal. This VB.NET tip describes how developers can use this method.

When you need to find a certain pattern of a string and modify the text to hide the contents, you can utilize VB.NET's Regex.Replace method. Regex.Replace is a very efficient way to replace values. It allows you to replace the string with a fixed string literal, and it provides a number of overloads that give developers the freedom to fully utilize this method.

In the example in Listing A, I look for a way to find a particular pattern in a string and replace it; supply a string literal that should replace the text in the example; and explore obscuring birth date data in a string with the generic text XX/XX/XXXX.

Notes about the code

I create the String variable, strTxt, to hold the value of the original text. I also define the Regex variable, rgxExp, to hold the value of the expression of the fixed string literal. I then set the text of the strTxt to some text that utilizes the data, which I plan to hide from display and utilize the Regex.Replace method to replace all occurrences of the dates of birth (DOB) data with the expression specified. The result is the text that shows the original text except for showing XX/XX/XXXX whenever a DOB format is met. I then display the resulting string in a message box.

Miss a tip?

Check out the Visual Basic archive, and catch up on the most recent editions of Irina Medvinskaya's column.

Advance your scripting skills to the next level with TechRepublic's free Visual Basic newsletter, delivered each Friday. Automatically sign up today!

  • Save
  • Print
  • 0

Print/View all Posts Comments on this article

The is a general comment on all the columns.tazmad  | 06/12/07
To print only the area you are interested inChaz Chance#  | 06/15/07
RE: Replace all occurrences of a pattern in a string with VB.NETJBo  | 06/17/07

What do you think?

Article Categories

Security
Security Solutions, IT Locksmith
Networking and Communications
E-mail Administration NetNote, Cisco Routers and Switches
CIO and IT Management
Project Management, CIO Issues, Strategies that Scale
Desktops, Laptops & OS
Windows 2000 Professional, Microsoft Word, Microsoft Excel, Microsoft Access, Windows XP,
Data Management
Oracle, SQL Server
Servers
Windows NT, Linux NetNote, Windows Server 2003
Career Development
Geek Trivia
Software/Web Development
Web Development Zone, Visual Basic, .NET

The PC Cracked Open

advertisement
Click Here