ExtensionMethod.NET Home of 880 C#, Visual Basic, F# and Javascript extension methods

AddRightIfDoesntExist

Adds a value onto the end of the string if it does not already exist there.

Source

''' <summary>
''' Adds a value onto the end of the string if it does not already exist there.
''' </summary>
''' <param name="buf"></param>
''' <param name="value"></param>
''' <returns></returns>
''' <remarks></remarks>
<Extension()> _
Public Function AddRightIfDoesntExist(ByVal buf As String, ByVal value As String) As String
    If buf.Length < value.Length Then
        Return buf
    End If

    If buf.Right(value.Length) = value Then
        Return buf
    Else
        Return buf & value
    End If
End Function

Example

Dim buf As String = CurDir.AddRightIfDoesntExist("\")
MsgBox(buf)

Author: Blake Pell

Submitted on: 27 jul. 2010

Language: VB

Type: System.String

Views: 4130