Most people know that, with a string in ASP.net or WinForms development, you can use the replace function to replace certain characters within a string with others. And, you probably know that you can even remove characters alltogether, using the replace function.
However, did you know there’s an easy way to replace multiple characters (different characters), using the replace function. OK – here’s the scenario. Let’s say you want to create a fairly unique number/string, using a date/time entry. As you know, a date/time ‘string’ has “/” characters, along with spaces, colons and “AM” or “PM”. BUT – all you want a final string of the numbers.
As it turns out, you can ‘daisy-chain’ replace functions. In other words, a normal replace would be something like this:
Dim num as string=Date.Now().ToString.Replace("/","")
This will completely remove the ‘/’ characters, as it replaces it with an empty string. But the date, as we said earlier has spaces in it, and colons and an AM/PM designation at the end. So, what we can do is to piggyback more replace functions at the end of the above function:
.Replace(":", "").Replace(" ", "").Replace("AM", "").Replace("PM", "")
This will remove all the colons, spaces and AM/PM designations and give you a perfect string of numbers.
All Things DotNet Discussed – Winforms/ASP.Net/SharePoint/WPF