How to match strings using wildcards in C# and VB.NET

To match strings using wildcards in C# and VB.NET you can use the following snippet.
It will internally convert the wildcard string to a Regex.

The Console-Output of this sample will be:

C:\Test\myFile01.xml
C:\Test\myFile02.xml

Sample C#

#region

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;

#endregion

namespace de.fesslersoft.RegexWildCard
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            var fileList = new List<String>();
            fileList.Add(@"C:\Test\myFile01.xml");
            fileList.Add(@"C:\Test\myFile02.xml");
            fileList.Add(@"C:\Test\myFolder01.xml");
            fileList.Add(@"C:\Test\myFolder02.xml");

            var wildCard = new Wildcard("*File*.xml", RegexOptions.IgnoreCase);
            foreach (var file in fileList.Where(file => wildCard.IsMatch(file)))
            {
                Console.WriteLine(file);
            }
            Console.Read();
        }
    }


    public class Wildcard : Regex
    {
        public Wildcard(string pattern) : base(ToRegex(pattern))
        {
        }

        public Wildcard(string pattern, RegexOptions options) : base(ToRegex(pattern), options)
        {
        }

        public static string ToRegex(string pattern)
        {
            return "^" + Escape(pattern).Replace("\\*", ".*").Replace("\\?", ".") + "$";
        }
    }
}

Sample VB.NET (autoconverted)

#Region ""

Imports System.Collections.Generic
Imports System.Linq
Imports System.Text.RegularExpressions

#End Region

Namespace de.fesslersoft.RegexWildCard
	Friend Class Program
		Private Shared Sub Main(args As String())
			Dim fileList = New List(Of [String])()
			fileList.Add("C:\Test\myFile01.xml")
			fileList.Add("C:\Test\myFile02.xml")
			fileList.Add("C:\Test\myFolder01.xml")
			fileList.Add("C:\Test\myFolder02.xml")

			Dim wildCard = New Wildcard("*File*.xml", RegexOptions.IgnoreCase)
			For Each file As var In fileList.Where(Function(file) wildCard.IsMatch(file))
				Console.WriteLine(file)
			Next
			Console.Read()
		End Sub
	End Class


	Public Class Wildcard
		Inherits Regex
		Public Sub New(pattern As String)
			MyBase.New(ToRegex(pattern))
		End Sub

		Public Sub New(pattern As String, options As RegexOptions)
			MyBase.New(ToRegex(pattern), options)
		End Sub

		Public Shared Function ToRegex(pattern As String) As String
			Return "^" + Escape(pattern).Replace("\*", ".*").Replace("\?", ".") + "$"
		End Function
	End Class
End Namespace

Please rate this snippet

How to Display an Image in Devexpress XtraGrid Control using C# and VB.NET

To Display an Image in Devexpress XtraGrid Control using C# and VB.NET see the example below.

Sample C#

#region

using System.Data;
using System.Drawing;
using System.Windows.Forms;
using WindowsFormsApplication77.Properties;
using DevExpress.XtraEditors.Repository;

#endregion

namespace de.fesslersoft.XtraGridImageTest
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            gridControl1.DataSource = CreateTable(3);
            gridView1.Columns["Image"].ColumnEdit = new RepositoryItemPictureEdit();
        }

        private DataTable CreateTable(int rowCount)
        {
            var image = Resources.Image1;
            var tbl = new DataTable();
            tbl.Columns.Add("ID", typeof (int));
            tbl.Columns.Add("URL", typeof (string));
            tbl.Columns.Add("Image", typeof (Image));
            for (var i = 0; i < rowCount; i++)
            {
                tbl.Rows.Add(new object[] {i, "http://codesnippets.fesslersoft.de", image});
            }
            return tbl;
        }
    }
}

Sample VB.NET

#Region ""

Imports System.Data
Imports System.Drawing
Imports System.Windows.Forms
Imports WindowsFormsApplication77.Properties
Imports DevExpress.XtraEditors.Repository

#End Region

Namespace de.fesslersoft.XtraGridImageTest
	Public Partial Class Form1
		Inherits Form
		Public Sub New()
			InitializeComponent()
			gridControl1.DataSource = CreateTable(3)
			gridView1.Columns("Image").ColumnEdit = New RepositoryItemPictureEdit()
		End Sub

		Private Function CreateTable(rowCount As Integer) As DataTable
			Dim image = Resources.Image1
			Dim tbl = New DataTable()
			tbl.Columns.Add("ID", GetType(Integer))
			tbl.Columns.Add("URL", GetType(String))
			tbl.Columns.Add("Image", GetType(Image))
			For i As var = 0 To rowCount - 1
				tbl.Rows.Add(New Object() {i, "http://codesnippets.fesslersoft.de", image})
			Next
			Return tbl
		End Function
	End Class
End Namespace

Result

XtraGrid ColumnImages

XtraGrid ColumnImages


Please rate this snippet

Generic Type Conversion snippet for C# and VB.NET

Here you can find a generic Type conversion snippet for C# and VB.NET.

Sample C#

public static T GenericConvert<T, X>(X value) where X : IConvertible
{
	return (T)Convert.ChangeType(value, typeof(T));
}

Sample VB.NET

Public Shared Function GenericConvert(Of T, X As IConvertible)(value As X) As T
	Return DirectCast(Convert.ChangeType(value, GetType(T)), T)
End Function

Please rate this snippet

How to reload a ViewController in IOS using Objective-C

To reload a UITableViewController in IOS using Objective-C you can simply use the reloadData method of the ViewController.

//MyTablViewController is a UITableViewController
[MyTableViewController.tableView reloadData] 

Please rate this snippet

How to create an alert dialog in Android

To create an alert dialog in Android you can use the following snippet.

Sample Java

new AlertDialog.Builder(this)
    .setTitle("Delete items")
    .setMessage("Are you sure you want to delete this items?")
    .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) { 
            // YES
        }
     })
    .setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) { 
            // NO
        }
     })
    .setIcon(android.R.drawable.ic_dialog_alert)
     .show();

Please rate this snippet

How to format a float to 2 decimal places in Objective-C

To format a float to 2 decimal places in Objective-C you can use the following snippet.

Sample Objective-C

NSString* formattedVariable = [NSString stringWithFormat:@"%.02f", myFloatVariable];

Please rate this snippet

How to convert string to int using Objective-C

To convert string to int using Objective-C you can use the snippet below.

Sample Objective-C

[myString intValue];

or you can do it like this.

myString.intValue;

Please rate this snippet

How to get XNode as Xml String with indentation in C# and VB.NET

Sample C#

public static String XNodeToString(XNode node, int indentation = 2)
{
	using (var sw = new StringWriter())
	{
		using (var xw = new XmlTextWriter(sw))
		{
			xw.Formatting = Formatting.Indented;
			xw.Indentation = indentation;
			node.WriteTo(xw);
		}
		return sw.ToString();
	}
}

Sample VB.NET

Public Shared Function XNodeToString(node As XNode, Optional indentation As Integer = 2) As String
	Using sw = New StringWriter()
		Using xw = New XmlTextWriter(sw)
			xw.Formatting = Formatting.Indented
			xw.Indentation = indentation
			node.WriteTo(xw)
		End Using
		Return sw.ToString()
	End Using
End Function

for more informations on indentation = 2 see XmlTextWriter.Indentation Property

Please rate this snippet

How to get XmlNode as Xml String with indentation in C# and VB.NET

Sample C#

public static String XmlNodeToString(XmlNode node, int indentation = 2)
{
	using (var sw = new StringWriter())
	{
		using (var xw = new XmlTextWriter(sw))
		{
			xw.Formatting = Formatting.Indented;
			xw.Indentation = indentation;
			node.WriteContentTo(xw);
		}
		return sw.ToString();
	}
}

Sample VB.NET

Public Shared Function XmlNodeToString(node As XmlNode, Optional indentation As Integer = 2) As String
	Using sw = New StringWriter()
		Using xw = New XmlTextWriter(sw)
			xw.Formatting = Formatting.Indented
			xw.Indentation = indentation
			node.WriteContentTo(xw)
		End Using
		Return sw.ToString()
	End Using
End Function

for more informations on indentation = 2 see XmlTextWriter.Indentation Property

Please rate this snippet

How to convert xml string to XmlNode in C# and VB.NET

To convert xml string to XmlNode in C# and VB.NET you can use the following snippet.

Sample C#

public static XmlNode XmlStringToXmlNode(string xmlInputString)
{
	if (String.IsNullOrEmpty(xmlInputString.Trim())) { throw new ArgumentNullException("xmlInputString"); }
	var xd = new XmlDocument();
	using (var sr = new StringReader(xmlInputString))
	{
		xd.Load(sr);
	}
	return xd;
}

Sample VB.NET

Public Shared Function XmlStringToXmlNode(xmlInputString As String) As XmlNode
	If String.IsNullOrEmpty(xmlInputString.Trim()) Then
		Throw New ArgumentNullException("xmlInputString")
	End If
	Dim xd = New XmlDocument()
	Using sr = New StringReader(xmlInputString)
		xd.Load(sr)
	End Using
	Return xd
End Function

Please rate this snippet