Substring a String from given start index and given substring length I want to substring a String from given start index and given substring length.


if the string is empty then returns String.Empty  
if the string length is greater than start index then it substrings from given start index to given length...
if the string length is less than start index then it returns  from start index to last index


I just want to know if it can be improved and shorter with all these checks.


if the string is null or empty
if the start index is not less than zero 
if the start index is less than the length of given input string.
if the given input string length is less than the desired substring length. 
if the given input string length is greater than the desired substring length. 


Sample Input and Ouput for my program:

SplitEntity("", 1, 15)               =&gt;   ""  
SplitEntity("abcdef", 0, 3)          =&gt;   "abc" 
SplitEntity("abcdef", 3, 100)        =&gt;   "def" 
SplitEntity("abcdef", 0, 100)        =&gt;   "abcdef"  
SplitEntity("abcdef", -1, 100)        =&gt;   ""


My function is below:

Public Function SplitEntity(entity As String, startIndex As Integer, 
                                       subStringLength As Integer) As String
            Dim spilttedString As String = String.Empty

            If (Not String.IsNullOrEmpty(entity) AndAlso startIndex &gt;=0 ) Then

                If (entity.Length &gt; startIndex) Then
                    If entity.Length &gt;= (startIndex + subStringLength) Then
                        spilttedString = entity.Substring(startIndex, subStringLength)

                    ElseIf entity.Length &lt; (startIndex + subStringLength) Then
                        spilttedString = entity.Substring(startIndex, (entity.Length - startIndex))
                    End If

                End If

            End If

            Return spilttedString

        End Function
for something like this I would suggest making it an Extension Method.

To that end I would suggest a name change as well.  Perhaps Substring_NoException.

To make it complete I think having the same overloads as Substring would help.

As for reducing/eliminating the conditional checks, one option is to use a Try/Catch block and simply catch every exception that Substring would and return an empty string except for the length being too long.

It might look something like this:

Imports System.Runtime.CompilerServices

    ''' &lt;summary&gt;
    ''' Extension method for &lt;c&gt;Substring&lt;/c&gt; to return empty strings instead of throwing exceptions
    ''' &lt;/summary&gt;
    ''' &lt;param name="startIndex"&gt;The zero-based character position of a substring in this instance&lt;/param&gt;
    '''
    &lt;Extension()&gt;
    Public Function Substring_NoException(input As String, startIndex As Integer) As String
        Return input.Substring_NoException(startIndex, input.Length - startIndex)
    End Function

    ''' &lt;summary&gt;
    ''' Extension method for &lt;c&gt;Substring&lt;/c&gt; to return empty strings instead of throwing exceptions
    ''' &lt;/summary&gt;
    ''' &lt;param name="startIndex"&gt;The zero-based character position of a substring in this instance&lt;/param&gt;
    ''' &lt;param name="length"&gt;The number of characters in a substring&lt;/param&gt;
    '''
    &lt;Extension()&gt;
    Public Function Substring_NoException(input As String, startIndex As Integer, length As Integer) As String
        Try
            Return input.Substring(startIndex, length)
        Catch ex As ArgumentOutOfRangeException
            If ex.ParamName = "length" AndAlso length &gt; input.Length - startIndex Then
                Return input.Substring(startIndex)
            End If
            Return String.Empty
        End Try
    End Function


usage is:

Dim testString As String = "abcdef"
Dim testString2 = testString.Substring_NoException(3, 100)


Returned value is:

testString2 = "def"