A really simple regular expression to strip out anything except alpha or numeric characters. In the following example \r, \n, !! and space are all non-alpha numerics.
Example characters that this could strip out
\n = new line
\r = carriage return
\" = quotation marks
\\ = Backslash
\t = tab
...
using System.Text.RegularExpressions;
....
string input = "This \r is a !! test \n";
string output = Regex.Replace(input, @"[^a-zA-Z0-9]", string.Empty);
//output = Thisisatest
By the way, using the @ symbol in fromt of a string means that you don't have to muck around with escape characters like \[]/. This is known as a verbatim string literal. Essentiall any string where you write @" at the beginning of the text in question (and close it off with ") means that all characters inside the quotation marks are treated exactly as they are typed.
Saturday, July 11, 2009
Friday, July 10, 2009
Code I love #1: IsNullOrEmpty(string)
String.IsNullOrEmpty(string)
Back in days of yore you would need to do something like
if (String.Empty) || (string = "") || (string = null)
{
//do stuff
}
Now you can cover off all those in one hit with String.IsNullOrEmpty(string).
(And yeah, code probably is probably wrong because I try not to use them since upgrading from vb6/ .net 1.0, but you get the idea :) )
Back in days of yore you would need to do something like
if (String.Empty) || (string = "") || (string = null)
{
//do stuff
}
Now you can cover off all those in one hit with String.IsNullOrEmpty(string).
(And yeah, code probably is probably wrong because I try not to use them since upgrading from vb6/ .net 1.0, but you get the idea :) )
Wednesday, July 8, 2009
Beginnings... on Agile & bug fixing
About 5 months ago I began the way overdue transfer from VB6/ .NET 1.0/ SQL 2000 to the new and exciting world of C# & VB.NET 3.5. Of course 4.0 is just around the corner with VS2010 being released, but still, I can write about stuff I'm learning :) I'm not expecting that I'll be writing about anything majorly interesting to anyone else, and I won't really be one for answering other people's questions. The stuff I write at work is all confidential, though the stuff I'm learning working in the Microsoft SDC is majorly awesome.
http://www.betterprojects.net/2009/06/email-whiteboards-and-well-caffeinated.html
Fantastic article on project management
1. Set Your Sights - defining the goal
2. The Real Team Energizer - making information critical to the project easily available to everyone in the project
3. Don’t Rush into Commitment
4. Ask Around - see what others have tried
Interesting article on how bugs are a failure in process and 3 questions we need to ask to improve ourselves as devs: (http://secretgeek.net )
1. Is this mistake somewhere else also?
2. What next bug is hidden behind this one?
3. What should I do to prevent bugs like this?
Great quote about agile development. Maybe I should put it on a t-shirt and walk around the office? "Agile is about surfacing pain. It's not about roses, perfume, and happy candlelight dinners together. " http://codebetter.com/
NotImplementedException is an awesome way of stubbing stuff out when you haven't figured out your return statement
http://www.betterprojects.net/2009/06/email-whiteboards-and-well-caffeinated.html
Fantastic article on project management
1. Set Your Sights - defining the goal
2. The Real Team Energizer - making information critical to the project easily available to everyone in the project
3. Don’t Rush into Commitment
4. Ask Around - see what others have tried
Interesting article on how bugs are a failure in process and 3 questions we need to ask to improve ourselves as devs: (http://secretgeek.net )
1. Is this mistake somewhere else also?
2. What next bug is hidden behind this one?
3. What should I do to prevent bugs like this?
Great quote about agile development. Maybe I should put it on a t-shirt and walk around the office? "Agile is about surfacing pain. It's not about roses, perfume, and happy candlelight dinners together. " http://codebetter.com/
NotImplementedException is an awesome way of stubbing stuff out when you haven't figured out your return statement
Subscribe to:
Posts (Atom)
