How to change the encoding of a String using C# and VB.NET

To change the encoding of a String using .NET you can use this Extension Method which is part of the Fesslersoft.Extensions. This method needs a source and a target encoding. Some people might find the source encoding parameter needless, but as Joel stated in his excellent blogpost

“It does not make sense to have a string without knowing what encoding it uses” (Joel Spolsky)

Samples

Sample C#

VB.NET Sample

If you have any questions or suggestions feel free to rate this snippet, post a comment or Contact Us via Email.

Related links:

Please rate this snippet

How to read a embedded resource to String in C# and VB.NET

To read a embedded resource to String in C# and VB.NET you can use the snippet below.
Keep in mind that this snippet is using Encoding.Default which might not always work in all situations correctly.

Sample C#

Sample VB.NET (autogenerated)

Please rate this snippet

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 read a String to Textreader using C# and VB.NET

To read a String to Textreader using C# and VB.NET you can use the snippet below.

Sample C#

using(TextReader textReader = new StringReader("string"))
{
    //...
}

Sample VB.NET

Using textReader As TextReader = New StringReader("string")
    '...
End Using

Please rate this snippet

How to split a delimited string to multiple rows using MSSQL

To split a delimited string to multiple rows using MSSQL you can use the snippet below.

Sample MSSQL

CREATE FUNCTION dbo.StringSplit
(
    @Input       NVARCHAR(MAX),
    @Delimiter  NVARCHAR(255)
)
RETURNS TABLE
AS
    RETURN (
		SELECT Number = ROW_NUMBER() OVER (ORDER BY Number),
        Item FROM (
					SELECT Number, Item = LTRIM(RTRIM(SUBSTRING(@Input, Number, CHARINDEX(@Delimiter, @Input + @Delimiter, Number) - Number)))
					FROM (SELECT ROW_NUMBER() OVER (ORDER BY ao.[object_id])
					FROM sys.all_objects AS ao CROSS APPLY sys.all_objects) AS n(Number)
					WHERE Number <= CONVERT(INT, LEN(@Input))
					AND SUBSTRING(@Delimiter + @Input, Number, 1) = @Delimiter
				  ) AS item);
GO

Please rate this snippet

How to trim a string using XSL 1.0

Since XSL 1.0 dows not have inbuild Trimming functions for Strings, you need to define your own templates.
You can use the Trim Templates shown in the codesnippet below.

Sample XSL

<xsl:variable name="whitespaceCharacters" select="'&#09;&#10;&#13; '" />
    
    <!-- Trim Right side of the String -->
    <xsl:template name="TrimRight">
        <xsl:param name="input" />
        <xsl:param name="trim" select="$whitespaceCharacters" />
        
        <xsl:variable name="length" select="string-length($input)" />
        <xsl:if test="string-length($input) &gt; 0">
            <xsl:choose>
                <xsl:when test="contains($trim, substring($input, $length, 1))">
                    <xsl:call-template name="TrimRight">
                        <xsl:with-param name="input" select="substring($input, 1, $length - 1)" />
                        <xsl:with-param name="trim" select="$trim" />
                    </xsl:call-template>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:value-of select="$input" />
                </xsl:otherwise>
            </xsl:choose>
        </xsl:if>
    </xsl:template>
    
    <!-- Trim Left side of the String -->
    <xsl:template name="TrimLeft">
        <xsl:param name="input" />
        <xsl:param name="trim" select="$whitespaceCharacters" />
        
        <xsl:if test="string-length($input) &gt; 0">
            <xsl:choose>
                <xsl:when test="contains($trim, substring($input, 1, 1))">
                    <xsl:call-template name="TrimLeft">
                        <xsl:with-param name="input" select="substring($input, 2)" />
                        <xsl:with-param name="trim" select="$trim" />
                    </xsl:call-template>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:value-of select="$input" />
                </xsl:otherwise>
            </xsl:choose>
        </xsl:if>
    </xsl:template>
    
    <!-- Trim both sides of the String -->
    <xsl:template name="Trim">
        <xsl:param name="input" />
        <xsl:param name="trim" select="$whitespaceCharacters" />
        <xsl:call-template name="TrimRight">
            <xsl:with-param name="input">
                <xsl:call-template name="TrimLeft">
                    <xsl:with-param name="input" select="$input" />
                    <xsl:with-param name="trim" select="$trim" />
                </xsl:call-template>
            </xsl:with-param>
            <xsl:with-param name="trim" select="$trim" />
        </xsl:call-template>
    </xsl:template>

Please rate this snippet

How to use substring in IOS using Objective-C

To use substring in IOS using Objective-C you can use the snippet below.

Sample Objective-C

NSString *myString = @"http://codesnippets.fesslersoft.de";
NSString *myNewString = [myString substringFromIndex:7]; //myNewString will be "codesnippets.fesslersoft.de"

Please rate this snippet

Case-Insensitive String.Replace in C# and VB.NET

Here you can find a Case-Insensitive String.Replace method for C# and VB.NET.

Sample C#

public static string ReplaceWithStringComparison(string input, string search, string replace, StringComparison comparison)
{
	var stringBuilder = new StringBuilder();
	var lastIndex = 0;
	var currIndex = input.IndexOf(search, comparison);
	while (currIndex != -1)
	{
		stringBuilder.Append(input.Substring(lastIndex, currIndex - lastIndex));
		stringBuilder.Append(replace);
		currIndex += search.Length;
		lastIndex = currIndex;
		currIndex = input.IndexOf(search, currIndex, comparison);
	}
	stringBuilder.Append(input.Substring(lastIndex));
	return stringBuilder.ToString();
}

Sample VB.NET

Public Shared Function ReplaceWithStringComparison(input As String, search As String, replace As String, comparison As StringComparison) As String
	Dim stringBuilder = New StringBuilder()
	Dim lastIndex = 0
	Dim currIndex = input.IndexOf(search, comparison)
	While currIndex <> -1
		stringBuilder.Append(input.Substring(lastIndex, currIndex - lastIndex))
		stringBuilder.Append(replace)
		currIndex += search.Length
		lastIndex = currIndex
		currIndex = input.IndexOf(search, currIndex, comparison)
	End While
	stringBuilder.Append(input.Substring(lastIndex))
	Return stringBuilder.ToString()
End Function

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 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