Adding extension method to string : Extension « Class « VB.Net






Adding extension method to string

  

Imports System
Imports System.Linq
Imports System.Runtime.CompilerServices




Module Module1
    Sub Main()
        Dim s As String = "this is a test"
        Console.WriteLine(s.SpaceToUnderscore())
    End Sub
End Module

    <Extension()> _
    Public Class Extensions

        <Extension()> _
        Public Shared Function SpaceToUnderscore(ByVal source As String) As String
            Dim cArray As Char() = source.ToCharArray()
            Dim result As String = String.Empty

            For Each c As Char In cArray
                If Char.IsWhiteSpace(c) Then
                    result = result & "_"
                Else
                    result = result & c
                End If
            Next

            Return result
        End Function
    End Class

   
    
  








Related examples in the same category