VB.Net/Class/Extension

Материал из VB Эксперт
Перейти к: навигация, поиск

Adding extension method to string

  
Imports System
Imports System.Linq
Imports System.Runtime.rupilerServices


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