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

How to check if a character is Uppercase in C# and VB.NET

To check if a character is Uppercase in C# and VB.NET you can use the following Extension Method

Sample C#

/// <summary>
///     Checks whether the character is an uppercase Character or not
/// </summary>
/// <param name="inputChar"></param>
/// <returns>True or False depending on the character</returns>
public static bool IsUpperCase(this char inputChar)
{
	return inputChar >= 'A' && inputChar <= 'Z';
}

Sample VB.NET

''' <summary>
'''     Checks whether the character is an uppercase Character or not
''' </summary>
''' <param name="inputChar"></param>
''' <returns>True or False depending on the character</returns>
<System.Runtime.CompilerServices.Extension> _
Public Shared Function IsUpperCase(inputChar As Char) As Boolean
	Return inputChar >= "A"C AndAlso inputChar <= "Z"C
End Function




How to use IF ELSE in an SQL Select query

In MSSQL IF ELSE is not supported inside the SQL-Query.
For a similiar behaviour you should use CASE WHEN keyword.

Sample Query

SELECT CASE 
            WHEN [Productnumber] LIKE '112%' THEN 'SOFTWARE'
			WHEN [Productnumber] LIKE '005%' THEN 'HARDWARE'
		    ELSE 'MISC' 
       END as [ProductType] 
FROM dbo.[Products]

IsUppercase extension method for C# and VB.NET

This snippet will give you the IsUppercase extension method for C# and VB.NET

Sample C#

public static bool IsUppercase(this string input)
{
	return Regex.IsMatch(input, @"^[A-Z]+$");
}

Sample VB.NET

<System.Runtime.CompilerServices.Extension> _
Public Shared Function IsUppercase(input As String) As Boolean
	Return Regex.IsMatch(input, "^[A-Z]+$")
End Function