Regular Expressions for Subversion keywords
Subversion March 28th, 2012
Here are different Regular Expressions to identify the common (to me at least), the Subversion keyword: $Id$ on different type of files (depending on how the comments go).
This one is for languages derived from C (like C++, C#, PHP, Java, JavaScript):
// \$Id\: [A-Za-z0-9\ \:\.\-\_]* \$
It will match something like:
// $Id: filename 3 2010-06-23 15:48:28Z username $
This one also for languages like C, but also, CSS:
/\* \$Id\: [A-Za-z0-9\ \:\.\-\_]* \$ \*/
It will match something like this:
/* $Id: filename 3 2010-06-23 15:48:28Z username $ */This one for Velocity files (as I’m using Castle MonoRail with NVelocity):
\#\# \$Id\: [A-Za-z0-9\ \:\.\-\_]* \$
That will match:
## $Id: filename 3 2010-06-23 15:48:28Z username $
So now, with these expressions, you can do some things like remove them from your code (if you are like me and are moving to Git).
Tags: C#, Castle, CSS, Git, JavaScript, regexp, Subversion, SVN
Remove duplicated whitespaces on C#
Programming October 28th, 2010
The Trim method on C# only removes whitespaces at the beginning and at the end of a string, but it does not remote duplicated whitespaces inside the string (like the Trim function on VB for Apps), which is very handy sometimes.
One of the ways to do it on C#, is using Regular Expressions. This method will not only remove whitespaces but also tabs and line breaks, and will replace them with one whitespace.
string s1 = "He saw a cute\tdog.\nThere\n\twas another sentence."; Regex r = new Regex(@"\s+"); string s2 = r.Replace(s1, @" "); // The result would be: He saw a cute dog. There was another sentence.
You will need to add the reference to System.Text.RegularExpressions to your namespace.
Source: Dot Net Perls
Convert a String to a Stream
Programming June 29th, 2010
In C#, to convert a String into a Stream object, we need to use the GetBytes method (from the Encoding.ASCII package), this way:
byte[] bytes = Encoding.ASCII.GetBytes(xmlContent);
And then, use that byte array when instantiate a Stream (for example, MemoryStream or FileStream):
MemoryStream stream = new MemoryStream(bytes);
Convertir un IList en un Array
Programming January 28th, 2010
Convertir un ArrayList a string[]
Programming November 30th, 2008
Diferencia entre ICollection e IList en C#
Programming October 30th, 2008
Los elementos dentro de un ICollection (para ser más exactos, los elementos dentro de una clase que implemente una interfaz ICollection) no tienen un orden específico. En un momento pueden estar ordenados de una manera, y en otro, sin alguna razón aparente, ordenados de otra forma.
En cambio, los elementos de un IList si tienen un orden establecido y relevante, como un arreglo. Esto permite agregar elementos en una ubicación específica. Además, los elementos van a permanecer en la posición asignada hasta que decidamos moverlos.
Además de estas dos interfaces, existe también IDictionary, que exitiende a ICollection. Este permite almacenar parejas de elementos (clave, valor), similar a como lo hace un diccionario de palabras (cada palabra tiene un significado). Al igual que en el ICollection, estos elementos no tienen un orden específico.
Tags: .Net, C#, ICollection, IDictionary, IList
Obtener el MimeType de un archivo desde C#
Programming October 24th, 2008
No he encontrado una manera directa de hacerlo, pero encontré esta función que busca la extensión del archivo en el registro de windows y si está registrada, obtiene su tipo mime.
private string getMimeType(string fileName) { string mimeType = "application/unknown"; string ext = System.IO.Path.GetExtension(fileName).ToLower(); Microsoft.Win32.RegistryKey regKey = Microsoft.Win32.Registry.ClassesRoot.OpenSubKey(ext); if (regKey != null && regKey.GetValue("Content Type") != null) mimeType = regKey.GetValue("Content Type").ToString(); return mimeType; }
Aún no la pruebo, pero según loque leí, funciona bien. Los post originales están acá y acá.
About