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

AddLeftIfDoesntExist

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

Source

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

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

Example

Dim buf As String = "Blake"
buf = buf.AddLeftIfDoesntExist("*")
MsgBox(buf)

Author: Blake Pell

Submitted on: 27 jul 2010

Language: VB

Type: System.String

Views: 2187