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

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"

How to TRIM a string in MSSQL

To TRIM a string in MSSQL you can use the following Userdefinedfunction Snippet.

Sample MSSQL

CREATE FUNCTION dbo.TRIM(@inputString VARCHAR(MAX))
RETURNS VARCHAR(MAX)
BEGIN
RETURN LTRIM(RTRIM(@inputString))
END

How to split a string into collection in VBA

To split a string into collection in VBA you can use the following snippet.

Sample VBA

Public Function SplitStringToCollection(sInput As String, Optional sSplitter As String = ";") As Collection
Dim lCounter As Long
Dim sTemp() As String

On Error GoTo errHandler
Set SplitStringToCollection = New Collection
sTemp = Split(sInput, sSplitter)
For lCounter = 0 To UBound(sTemp)
    If (CStr(Replace(sTemp(lCounter), sSplitter, vbNullString)) <> vbNullString) Then
        SplitStringToCollection.Add CStr(Replace(sTemp(lCounter), sSplitter, vbNullString))
    End If
Next lCounter
Exit Function

errHandler:
Set SplitStringToCollection = New Collection
'handle the exception your way
End Function

How to cut a string to full words in C# and VB.NET

To cut a string to full words you can use the following snippet.

Sample C#

public string Chop(string input, int maxLength, string chopChars="...", char spaceChar = ' ')
{
	var strings = input.Split(new[] { spaceChar }, StringSplitOptions.RemoveEmptyEntries);
	if (strings[0].Length > maxLength)
	{
		return strings[0];
	}
	var builder = new StringBuilder();
	foreach (var s in strings)
	{
		if ((builder + s).Length > maxLength)
		{
			return string.Format("{0}{1}", builder.ToString().TrimEnd(spaceChar), chopChars);
		}
		builder.Append(s + spaceChar);
	}
	return string.Format("{0}{1}", builder.ToString().TrimEnd(spaceChar), chopChars);
}

Sample VB.NET

Public Function Chop(input As String, maxLength As Integer, Optional chopChars As String = "...", Optional spaceChar As Char = " "C) As String
	Dim strings = input.Split(New () {spaceChar}, StringSplitOptions.RemoveEmptyEntries)
	If strings(0).Length > maxLength Then
		Return strings(0)
	End If
	Dim builder = New StringBuilder()
	For Each s As var In strings
		If (builder + s).Length > maxLength Then
			Return String.Format("{0}{1}", builder.ToString().TrimEnd(spaceChar), chopChars)
		End If
		builder.Append(s + spaceChar)
	Next
	Return String.Format("{0}{1}", builder.ToString().TrimEnd(spaceChar), chopChars)
End Function